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
31 changes: 31 additions & 0 deletions configurationapp/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
23 changes: 23 additions & 0 deletions configurationapp/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>configurationapp</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
4 changes: 4 additions & 0 deletions configurationapp/.settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding/<project>=UTF-8
5 changes: 5 additions & 0 deletions configurationapp/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8
4 changes: 4 additions & 0 deletions configurationapp/.settings/org.eclipse.m2e.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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;

@Service
public class ConfigurationDao {
private class IdProvider {
Expand All @@ -34,15 +35,43 @@ public ConfigurationDao() {
}

public List<ConfigValue> getConfigurationsForYearMonth(String yearMonth) {
if(isConfigurationKeyExist(yearMonth)){
return currentConfigurations.get(yearMonth);
}
return new ArrayList<>();
}

public void addConfiguration(String yearMonth, ConfigValue value) {
int newId = idProvider.getNextId();

value.setConfigId(newId);
if(isConfigurationKeyExist(yearMonth)){
List<ConfigValue> values= currentConfigurations.get(yearMonth);
values.add(value);
currentConfigurations.put(yearMonth, values);
}else{
List<ConfigValue> values = new ArrayList<>();
values.add(value);
currentConfigurations.put(yearMonth, values);
}
}

public void removeAllConfigurationsForYearMonth(String yearMonth) {

if(isConfigurationKeyExist(yearMonth)){
currentConfigurations.remove(yearMonth);
}else{
throw new RuntimeException("Configuration does not exist");
}

}

public void removeSelecteConfigurationForYearMonth(String yearMonth, int configId){
if(isConfigurationKeyExist(yearMonth)){
List<ConfigValue> values = currentConfigurations.get(yearMonth);
values.removeIf(value -> value.getConfigId() == configId);
}
}

private boolean isConfigurationKeyExist(String key){
return currentConfigurations.containsKey(key);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.att.data.configurations;

import javax.validation.constraints.NotNull;

/**
* Data Model
*/
public class ConfigValue {

@NotNull(message = "config name must not be null")
private String configName;
private int configId;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,77 @@
package com.att.web.configuarations;

import com.att.dao.configurations.ConfigurationDao;
import com.att.data.configurations.ConfigValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.att.dao.configurations.ConfigurationDao;
import com.att.data.configurations.ConfigValue;

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


private static Logger logger = LoggerFactory.getLogger(ConfigurationController.class);
private ConfigurationDao dao;

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

@RequestMapping(value="/{yearMonthNumber}", method=RequestMethod.GET)
@RequestMapping(value="/get/{yearMonthNumber}", method=RequestMethod.GET)
@ResponseBody
public List<ConfigValue> getConfigurationsForYearMonth(
@PathVariable("yearMonthNumber") String yearMonth) {


if(yearMonth != null && !yearMonth.isEmpty()){
return dao.getConfigurationsForYearMonth(yearMonth);
}

return new ArrayList<>();
}

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

if(yearMonth != null && !yearMonth.isEmpty()){
dao.removeAllConfigurationsForYearMonth(yearMonth);
}
} catch (Exception ex) {

logger.error(ex.getMessage());
}
}


@RequestMapping(value="/delete/{yearMonthNumber}/{configId}", method=RequestMethod.DELETE)
public void deleteSelectedConfigurationsForYearMonth(@PathVariable("yearMonthNumber") String yearMonth,
@PathVariable("configId") Integer configurationId) {

if(yearMonth != null && !yearMonth.isEmpty() && configurationId != null){
dao.removeSelecteConfigurationForYearMonth(yearMonth,configurationId);

}
}

@RequestMapping(value="/{yearMonthNumber}", method={ RequestMethod.POST, RequestMethod.PUT })
@RequestMapping(value="/add/{yearMonthNumber}", method={RequestMethod.POST}, consumes=MediaType.APPLICATION_JSON_VALUE)
public void addConfigurationForYearMonth(
@PathVariable("yearMonthNumber") String yearMonth,
@RequestBody ConfigValue value) {
@Valid @RequestBody ConfigValue value) {
if(yearMonth != null && !yearMonth.isEmpty()){
dao.addConfiguration(yearMonth, value);
}

}
}
124 changes: 118 additions & 6 deletions configurationapp/src/main/resources/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head>
<title>Configuration App</title>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script src="js/app.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" />
Expand All @@ -20,13 +20,15 @@ <h1>Welcome To The Configuration App</h1>
<option value="062018">Jun 2018</option>
<option value="072018">Jul 2018</option>
</select>
<input type="text" value="" placeholder="Add New Configuration Here" id="newConfig"/>
</div>
<div class="config-table-wrapper">
<table id="configTable">
<thead>
<tr>
<td>Configuration Id</td>
<td>Configuration Name</td>
<td></td>
</tr>
</thead>
<tbody>
Expand All @@ -36,12 +38,122 @@ <h1>Welcome To The Configuration App</h1>
</div>
<div class="controls">
<button id="saveButton">Save Configurations</button>
<button id="deleteButton">Delete Configurations</button>
</div>
</div>
<script>
$(document).ready(function() {
app.init();
})
</script>
<script>
$(document).ready(function() {
app.init();
$("#saveButton").click(function() {
addConfiguration();
});

$("#timePeriod").change(function(){
getConfiguration();
});

$("#deleteButton").click(function(){
deleteConfiguration();
});


var table = $('#configTable').DataTable();
$('#configTable tbody').on( 'click', 'button', function () {
var data = table.row( $(this).parents('tr') ).data();
table.row( $(this).parents('tr') )
.remove()
.draw();
deleteSelectedConfiguration(data[0]);
});

});

function addConfiguration() {

var newConfigName = $("#newConfig").val();
if (newConfigName === undefined) {
alert("Please enter a Configuration");
return;
}

var config = {
configName : newConfigName,
configId : 0
}

var selectedTime = $("#timePeriod option:selected").val();

$.ajax({
type : "POST",
contentType : "application/json",
url : "configuration/add/" + selectedTime,
data : JSON.stringify(config),
cache : false,
success : function(data) {
getConfiguration();
},
error : function(data) {
console.log("error:" + JSON.stringify(data));
}
});

}


function getConfiguration(){
var selectedTime = $("#timePeriod option:selected").val();
var dataTable = $('#configTable').DataTable();
dataTable.clear().draw();
$.ajax({
type : "GET",
contentType : "application/json",
url : "configuration/get/"+selectedTime,
cache : false,
success: function (data) {
$.each(data, function(index, configValue) {
dataTable.row.add([
configValue.configId,
configValue.configName]).draw();
});

},
error: function (e) {
console.log("error:"+JSON.stringify(e));
}
});
}

function deleteConfiguration(){
var selectedTime = $("#timePeriod option:selected").val();
$.ajax({
type : "DELETE",
contentType : "application/json",
url : "configuration/delete/"+selectedTime,
cache : false,
success: function (data) {
getConfiguration();
},
error: function (e) {
console.log("error:"+JSON.stringify(e));
}
});
}

function deleteSelectedConfiguration(configId){
var selectedTime = $("#timePeriod option:selected").val();
$.ajax({
type : "DELETE",
contentType : "application/json",
url : "configuration/delete/"+selectedTime+"/"+configId,
cache : false,
success: function (data) {
getConfiguration();
},
error: function (e) {
console.log("error:"+JSON.stringify(e));
}
});
}
</script>
</body>
</html>
Loading