diff --git a/configurationapp/src/main/java/com/att/dao/configurations/ConfigurationDao.java b/configurationapp/src/main/java/com/att/dao/configurations/ConfigurationDao.java index f889558..7840ef2 100644 --- a/configurationapp/src/main/java/com/att/dao/configurations/ConfigurationDao.java +++ b/configurationapp/src/main/java/com/att/dao/configurations/ConfigurationDao.java @@ -1,15 +1,17 @@ package com.att.dao.configurations; import com.att.data.configurations.ConfigValue; +import com.att.web.configuarations.ConfigurationController; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; @Service public class ConfigurationDao { + private static final Logger logger = LoggerFactory.getLogger(ConfigurationDao.class); + private class IdProvider { private int currentId; @@ -33,16 +35,72 @@ public ConfigurationDao() { currentConfigurations = new HashMap<>(); } + public void addConfigurationforYearMonth(String yearMonth, ConfigValue value) { + try { + if (value != null && !isNullOrEmpty(yearMonth)) { + value.setConfigId(idProvider.getNextId()); + + List configurationList = currentConfigurations.get(yearMonth); + if (isConfigExist(yearMonth)) { + Optional existingConfigValue = configurationList + .stream() + .filter(data -> data.getConfigName().equals(value.getConfigName())) + .findFirst(); + + if (!existingConfigValue.isPresent()) { + configurationList.add(value); + currentConfigurations.put(yearMonth, configurationList); + } + } else { + configurationList = new ArrayList(); + configurationList.add(value); + currentConfigurations.put(yearMonth, configurationList); + } + } + } catch (Exception e) { + logger.error("Exception adding configuration for selected year and month. " + e.getMessage()); + } + } + + public List getConfigurationsForYearMonth(String yearMonth) { + try { + if (isConfigExist(yearMonth) && !isNullOrEmpty(yearMonth)) { + return currentConfigurations.get(yearMonth); + } + } catch (Exception e) { + logger.error("Error fetching configurations for selected year and month. " + e.getMessage()); + } return new ArrayList<>(); } - public void addConfiguration(String yearMonth, ConfigValue value) { - int newId = idProvider.getNextId(); + public void deleteAllConfigurationsForYearMonth(String yearMonth) { + + try { + if (!isNullOrEmpty(yearMonth)) { + currentConfigurations.remove(yearMonth); + } + } catch (Exception e) { + logger.error("Error deleting config for year and month " + e.getMessage()); + } } - public void removeAllConfigurationsForYearMonth(String yearMonth) { + public void deleteSelectedConfigForYearMonth(String yearMonth, int configId) { + try { + if (!isNullOrEmpty(yearMonth) && isConfigExist(yearMonth)) { + currentConfigurations.get(yearMonth).removeIf(it -> it.getConfigId() == configId); + } + } catch (Exception e) { + logger.error("Error deleting selected config for year and month. " + e.getMessage()); + } + } + + public static boolean isNullOrEmpty(String str) { + return !(str != null && !str.trim().isEmpty()); + } + private boolean isConfigExist(String key) { + return currentConfigurations.containsKey(key); } } diff --git a/configurationapp/src/main/java/com/att/web/configuarations/ConfigurationController.java b/configurationapp/src/main/java/com/att/web/configuarations/ConfigurationController.java index 995402a..55b84ed 100644 --- a/configurationapp/src/main/java/com/att/web/configuarations/ConfigurationController.java +++ b/configurationapp/src/main/java/com/att/web/configuarations/ConfigurationController.java @@ -2,6 +2,8 @@ import com.att.dao.configurations.ConfigurationDao; import com.att.data.configurations.ConfigValue; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @@ -9,37 +11,67 @@ import java.util.List; @RestController -@RequestMapping(value="/configuration") +@RequestMapping(value = "/configuration") public class ConfigurationController { private ConfigurationDao dao; + private static final Logger logger = 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 getConfigurationsForYearMonth( @PathVariable("yearMonthNumber") String yearMonth) { - return new ArrayList<>(); + List response = new ArrayList(); + try { + logger.debug("Fetching configuration for year month : ${yearMonth}"); + response = dao.getConfigurationsForYearMonth(yearMonth); + } catch (Exception e) { + logger.error("Error fetching configuration for year month : ${yearMonth}. :: " + this.getClass().getName()); + } + return response; } - @RequestMapping(value="/{yearMonthNumber}", method=RequestMethod.DELETE) + @RequestMapping(value = "/{yearMonthNumber}", method = RequestMethod.DELETE) public void deleteConfigurationsForYearMonth(@PathVariable("yearMonthNumber") String yearMonth) { try { + logger.debug("Deleting configuration for year month : ${yearMonth}"); + dao.deleteAllConfigurationsForYearMonth(yearMonth); } catch (Exception ex) { + logger.error("Error deleting configurations for selected year and month. Class :: " + this.getClass().getName()); + } + } + @RequestMapping(value = "/{yearMonthNumber}/{configId}", method = RequestMethod.DELETE) + public void deleteSelectedConfigForYearMonth(@PathVariable("yearMonthNumber") String yearMonth, + @PathVariable("configId") Integer configId) { + try { + logger.debug("Deleting selected configuration for year month : ${yearMonth}"); + dao.deleteSelectedConfigForYearMonth(yearMonth, configId); + } catch (Exception e) { + logger.error("Error deleting selected config for year and month. :: " + this.getClass().getName()); } } - @RequestMapping(value="/{yearMonthNumber}", method={ RequestMethod.POST, RequestMethod.PUT }) + @RequestMapping(value = "/{yearMonthNumber}", method = {RequestMethod.POST}) public void addConfigurationForYearMonth( @PathVariable("yearMonthNumber") String yearMonth, @RequestBody ConfigValue value) { + try { + logger.debug("Adding configuration for selected year and month : ${yearMonth}"); + dao.addConfigurationforYearMonth(yearMonth, value); + } catch (Exception e) { + logger.error("Error adding configuration for ${yearMonth}. :: " + this.getClass().getName()); + } + + } } diff --git a/configurationapp/src/main/resources/static/css/main.css b/configurationapp/src/main/resources/static/css/main.css index d03098f..4cd2df9 100644 --- a/configurationapp/src/main/resources/static/css/main.css +++ b/configurationapp/src/main/resources/static/css/main.css @@ -1,5 +1,5 @@ .controls { - + padding-bottom: 10px; } .config-table-wrapper { @@ -8,4 +8,18 @@ .content { +} + +table, td, th { + border: 1px solid #ddd; + text-align: left; +} + +table { + border-collapse: collapse; + width: 100%; +} + +th, td { + padding: 15px; } \ No newline at end of file diff --git a/configurationapp/src/main/resources/static/index.html b/configurationapp/src/main/resources/static/index.html index 7cd14ba..48ae45e 100644 --- a/configurationapp/src/main/resources/static/index.html +++ b/configurationapp/src/main/resources/static/index.html @@ -2,46 +2,67 @@ Configuration App - + - + + -

Welcome To The Configuration App

-
-
- -
-
- - - - - - - - +

Welcome To The Configuration App

+
+
+ +
+
+ + + +
+
+
Configuration IdConfiguration Name
+ + + + + + + + - -
Configuration IdConfiguration NameAction
-
-
- -
+ +
- +
+ +
+ + \ No newline at end of file diff --git a/configurationapp/src/main/resources/static/js/app.js b/configurationapp/src/main/resources/static/js/app.js index 2d4b945..622cf82 100644 --- a/configurationapp/src/main/resources/static/js/app.js +++ b/configurationapp/src/main/resources/static/js/app.js @@ -1,25 +1,124 @@ -(function() { - function App() { +(function () { + function App () { } - App.prototype.getData = function() { - $.ajax('',{ - - }).then(function(data) { + App.prototype.getData = function () { + $.ajax('', {}).then(function (data) { }) }; - App.prototype.init = function() { - $('#configTable').DataTable({ + App.prototype.init = function () { + var table = $('#configTable').DataTable({ scrollY: 300, paging: false, sorting: false, searching: false, - info: false + info: false, + columns: [ + {'data': 'configId'}, + {'data': 'configName'}, + { + 'data': null, + 'defaultContent': '', + 'targets': -1 + } + ] + }); + + $('#configTable tbody').on('click', 'button', function () { + var data = table.row($(this).parents('tr')).data(); + deleteSelectedConfigForMonthYear(data.configId); }); }; window.app = new App; -})($); \ No newline at end of file +})($); + +function getConfigMonthYear () { + var selectedTimePeriod = $('#timePeriod option:selected').val(); + $.ajax({ + type: 'GET', + contentType: 'application/json', + url: 'configuration/' + selectedTimePeriod, + dataType: 'json', + cache: false, + success: function (data) { + updateConfigTable(data); + }, + error: function (data) { + console.log('Error fetching configurations for selected month and year. ' + JSON.stringify(data)); + } + }); +} + +function updateConfigTable (data) { + $ConfigTable = $('#configTable').DataTable(); + $ConfigTable.clear(); + $ConfigTable.rows.add(data); + $ConfigTable.draw(); +} + +function addNewConfigurationForYearMonth () { + var selectedTimePeriod = $('#timePeriod option:selected').val(); + var configName = $('#newConfigName').val(); + + if (configName != null && configName.trim().length > 0) { + + var configValue = {}; + configValue['configName'] = configName; + + $.ajax({ + type: 'POST', + contentType: 'application/json', + url: 'configuration/' + selectedTimePeriod, + data: JSON.stringify(configValue), + cache: false, + success: function (data) { + getConfigMonthYear(); + }, + error: function (data) { + alert('Error adding configuration to selected year and method'); + console.log('Error adding configuration to selected year and method. ' + JSON.stringify(data)); + } + }); + } else { + alert('Please enter configuration name'); + } +} + +function deleteAllConfigForMonthYear () { + var selectedTimePeriod = $('#timePeriod option:selected').val(); + + $.ajax({ + type: 'DELETE', + contentType: 'application/json', + url: 'configuration/' + selectedTimePeriod, + cache: false, + success: function (data) { + getConfigMonthYear(); + }, + error: function (e) { + alert("Error deleting configuration for selected year and method"); + console.log('Error deleting configurations for selected year and month.' + JSON.stringify(e)); + } + }); +} + +function deleteSelectedConfigForMonthYear (configId) { + var selectedTimePeriod = $('#timePeriod option:selected').val(); + + $.ajax({ + type: 'DELETE', + url: 'configuration/' + selectedTimePeriod + '/' + configId, + cache: false, + success: function (data) { + getConfigMonthYear(); + }, + error: function (e) { + alert("Error deleting selected configuration"); + console.log('Error deleting selected configurations for selected year and month.' + JSON.stringify(e)); + } + }); +} \ No newline at end of file