Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions configurationapp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.att</groupId>
<artifactId>configurationapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.0.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>configurationapp</name>
Expand Down Expand Up @@ -39,16 +39,39 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>configurationapp</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
<finalName>configurationapp</finalName>
</build>


Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package com.att.dao.configurations;

import com.att.data.configurations.ConfigValue;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.att.data.configurations.ConfigValue;

/**
* Data access layer implementation with basic CRUD operations, stores the data
* in memory
*
*/
@Service
public class ConfigurationDao {

private class IdProvider {
private int currentId;

Expand All @@ -26,23 +33,123 @@ public int getNextId() {
* No DB, so store the configs in a map.
*/
private Map<String, List<ConfigValue>> currentConfigurations;

/**
* Internal class to generate IDs. Similar to a database sequence.
*/
private IdProvider idProvider;

private static final String NO_NULLS_MSG = "Null values are not allowed";

public ConfigurationDao() {
idProvider = new IdProvider();
currentConfigurations = new HashMap<>();

init();

}

/**
* Returns a list of configurations for the selected month
*
* @param yearMonth
* @return
*/
public List<ConfigValue> getConfigurationsForYearMonth(String yearMonth) {
return new ArrayList<>();
if (yearMonth == null) {
throw new IllegalArgumentException(NO_NULLS_MSG);
}

return currentConfigurations.get(yearMonth);
}

/**
* Adds a single configuration, does not check for duplicates.
*
* @param yearMonth
* @param value
*/
public void addConfiguration(String yearMonth, ConfigValue value) {
int newId = idProvider.getNextId();

if (yearMonth == null || value == null) {
throw new IllegalArgumentException(NO_NULLS_MSG);
}

if (value.getConfigId() == -1) {
// preserve existing, but assign new if does not exist
value.setConfigId(idProvider.getNextId());

}
List<ConfigValue> list = currentConfigurations.get(yearMonth);
if (list == null) {
list = new ArrayList<ConfigValue>();
}
list.add(value);
currentConfigurations.put(yearMonth, list);
}

/**
* Removes all the configurations for the selected month
*
* @param yearMonth
*/
public void removeAllConfigurationsForYearMonth(String yearMonth) {
if (yearMonth != null) {
currentConfigurations.remove(yearMonth);
}
}

/**
* Removes all the configurations for the selected month
*
* @param yearMonth
*/
public void removeConfiguration(String yearMonth, int id) {
if (yearMonth != null) {
List<ConfigValue> list = currentConfigurations.get(yearMonth);
for (ConfigValue c : list) {
if (c.getConfigId() == id) {
list.remove(c);
break;
}
}
}
}

/**
* Removes all the configurations.
*/
public void removeAllConfigurations() {
currentConfigurations = new HashMap<>();
}

/**
* Returns a map of all configurations
*
* @return
*/
public Map<String, List<ConfigValue>> getCurrentConfigurations() {
// it should be a DB call here, so let's not bother with creating a deep copy
return currentConfigurations;
}

private void init() {
// should be equivalent to initial database call with a default query
List<ConfigValue> list1 = new ArrayList<>();

list1.add(new ConfigValue("A", idProvider.getNextId()));
list1.add(new ConfigValue("B", idProvider.getNextId()));
list1.add(new ConfigValue("C", idProvider.getNextId()));
list1.add(new ConfigValue("D", idProvider.getNextId()));
currentConfigurations.put("012018", list1);

List<ConfigValue> list2 = new ArrayList<>();
list2.add(new ConfigValue("A", idProvider.getNextId()));
list2.add(new ConfigValue("C", idProvider.getNextId()));
list2.add(new ConfigValue("F", idProvider.getNextId()));
list2.add(new ConfigValue("G", idProvider.getNextId()));
list2.add(new ConfigValue("H", idProvider.getNextId()));
currentConfigurations.put("022018", list2);
}

}
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
package com.att.data.configurations;

/**
* Data Model
* Data Model, contains configuration name and configuration id values. Id is
* equal to -1 by default if not provided.
*/
public class ConfigValue {
private String configName;
private int configId;
private int configId = -1; // not a required field

public ConfigValue(String name, int id) {
checkName(name);
this.configId = id;
this.configName = name;
}

public ConfigValue() {

public ConfigValue(String name) {
checkName(name);
this.configName = name;
}

public void setConfigName(String name) {
checkName(name);
this.configName = name;
}

private void checkName(String name) {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("null or empty cofig name is not allowed");
}
}

public String getConfigName() {
return this.configName;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,101 @@
package com.att.web.configuarations;

import com.att.dao.configurations.ConfigurationDao;
import com.att.data.configurations.ConfigValue;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import com.att.dao.configurations.ConfigurationDao;
import com.att.data.configurations.ConfigValue;

@RestController
@RequestMapping(value="/configuration")
@RequestMapping(value = "/configuration")
public class ConfigurationController {

private static final String ERROR_OCCURED = "Error occured";
private ConfigurationDao dao;
Logger log = LoggerFactory.getLogger(ConfigurationController.class);

@Autowired
public ConfigurationController(ConfigurationDao dao) {
this.dao = dao;
}

@RequestMapping(value="/{yearMonthNumber}", method=RequestMethod.GET)
@RequestMapping(value = "/{yearMonthNumber}", method = RequestMethod.GET)
@ResponseBody
public List<ConfigValue> getConfigurationsForYearMonth(
@PathVariable("yearMonthNumber") String yearMonth) {
public List<ConfigValue> getConfigurationsForYearMonth(@PathVariable("yearMonthNumber") String yearMonth) {
try {
log.debug("inside getConfigurationsForYearMonth, yearMonth=" + yearMonth);
} catch (Exception ex) {
log.error(ERROR_OCCURED, ex);
}

return new ArrayList<>();
return dao.getConfigurationsForYearMonth(yearMonth);
}

@RequestMapping(value="/{yearMonthNumber}", method=RequestMethod.DELETE)
@RequestMapping(value = "/{yearMonthNumber}", method = RequestMethod.DELETE)
public void deleteConfigurationsForYearMonth(@PathVariable("yearMonthNumber") String yearMonth) {
try {
log.debug("inside deleteConfigurationsForYearMonth");

dao.removeAllConfigurationsForYearMonth(yearMonth);

} catch (Exception ex) {
log.error(ERROR_OCCURED, ex);
}
}

@RequestMapping(value = "/{yearMonthNumber}/{id}", method = RequestMethod.DELETE)
public void deleteConfiguration(@PathVariable("yearMonthNumber") String yearMonth, @PathVariable("id") int id) {
try {
log.debug("inside deleteConfiguration");

dao.removeConfiguration(yearMonth, id);

} catch (Exception ex) {
log.error(ERROR_OCCURED, ex);
}
}

@RequestMapping(value="/{yearMonthNumber}", method={ RequestMethod.POST, RequestMethod.PUT })
public void addConfigurationForYearMonth(
@PathVariable("yearMonthNumber") String yearMonth,
@RequestBody ConfigValue value) {
@RequestMapping(value = "/{yearMonthNumber}", method = { RequestMethod.POST, RequestMethod.PUT })
public void addConfigurationForYearMonth(@PathVariable("yearMonthNumber") String yearMonth,
@RequestParam String configName) {
try {
log.debug("inside addConfigurationForYearMonth");
dao.addConfiguration(yearMonth, new ConfigValue(configName));
} catch (Exception ex) {
log.error(ERROR_OCCURED, ex);
}
}

@RequestMapping(method = RequestMethod.POST)
public void addConfiguration(@RequestParam String configName) {
try {
// default selection with all months
addConfigurationForYearMonth("", configName);
} catch (Exception ex) {
log.error(ERROR_OCCURED, ex);
}
}

@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public Map<String, List<ConfigValue>> getCurrentConfigurations() {
try {
log.debug("inside getCurrentConfigurations");

return dao.getCurrentConfigurations();
} catch (Exception ex) {
log.error(ERROR_OCCURED, ex);
return null;
}
}
}
3 changes: 2 additions & 1 deletion configurationapp/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
server.port=9000
spring.mvc.favicon.enabled=false

logging.level.root=WARN
logging.level.root=ERROR
logging.level.com.att=DEBUG
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
5 changes: 5 additions & 0 deletions configurationapp/src/main/resources/static/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@

.content {

}

.tdstyle {
width: 30%;
text-align: center;
}
Loading