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
12 changes: 9 additions & 3 deletions configurationapp/src/main/java/com/att/ConfigurationApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* The main class of ConfigurationApp
*
* @author gayathri
*
*/
@SpringBootApplication
public class ConfigurationApp {

public static void main(String[] args) {
SpringApplication.run(ConfigurationApp.class, args);
}
public static void main(String[] args) {
SpringApplication.run(ConfigurationApp.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -1,48 +1,195 @@
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 java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.att.data.configurations.ConfigValue;

/**
* The ConfigurationDao class is used to manage the month year wise
* configurations.
*
* @author gayathri
*
*/
@Service
public class ConfigurationDao {
private class IdProvider {
private int currentId;

public IdProvider() {
currentId = 0;
}
/**
* Simple Class to provide sequence id to configurations.
*
* @author gayathri
*
*/
private class IdProvider {
private int currentId;

/**
* Constructor initialization with sequence to zero.
*/
public IdProvider() {
currentId = 0;
}

public int getNextId() {
return this.currentId++;
}
}
/**
* To get next sequence id for the configuration.
*
* @return next sequence id
*/
public int getNextId() {
return this.currentId++;
}
}

/**
* No DB, so store the configs in a map.
*/
private Map<String, List<ConfigValue>> currentConfigurations;
private IdProvider idProvider;
/**
* No DB, so store the configs in a map.
*/
private Map<String, List<ConfigValue>> currentConfigurations;
private IdProvider idProvider;
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationDao.class);

public ConfigurationDao() {
idProvider = new IdProvider();
currentConfigurations = new HashMap<>();
}
/**
* Initializing default constructor.
*/
public ConfigurationDao() {
idProvider = new IdProvider();
currentConfigurations = new HashMap<>();
}

public List<ConfigValue> getConfigurationsForYearMonth(String yearMonth) {
return new ArrayList<>();
}
/**
* To get the configurations of the given month.
*
* @param yearMonth year month like 0520119 for May 2019.
* @return List<ConfigValue>
*/
public List<ConfigValue> getConfigurationsForYearMonth(String yearMonth) {
try {
LOGGER.debug("START : getConfigurationsForYearMonth(String) of " + this.getClass().getSimpleName());
if (currentConfigurations != null && yearMonth != null && yearMonth.trim().length() > 0) {
List<ConfigValue> configurations = currentConfigurations.get(yearMonth);
if (configurations == null) {
configurations = new ArrayList<ConfigValue>();
}
LOGGER.debug("STOP : getConfigurationsForYearMonth(String) of " + this.getClass().getSimpleName());
return configurations;
}
} catch (Exception e) {
LOGGER.error("EXCEPTION : getConfigurationsForYearMonth(String) of " + this.getClass().getSimpleName());
}
return new ArrayList<ConfigValue>();
}

public void addConfiguration(String yearMonth, ConfigValue value) {
int newId = idProvider.getNextId();
/**
* To add the new configuration for the given month year.
*
* @param yearMonth year month like 0520119 for May 2019.
* @param value Config Value to be added.
* @return result
*/
public String addConfiguration(String yearMonth, ConfigValue value) {
String result = "Oops! Something went wrong while adding new configuration.";
try {
LOGGER.debug("START : addConfiguration(String, ConfigValue) of " + this.getClass().getSimpleName());
if (currentConfigurations != null && yearMonth != null && yearMonth.trim().length() > 0 && value != null) {
int newId = idProvider.getNextId();
value.setConfigId(newId);

}
List<ConfigValue> configurations = currentConfigurations.get(yearMonth);
if (configurations == null) {
configurations = new ArrayList<ConfigValue>();
configurations.add(value);
currentConfigurations.put(yearMonth, configurations);
result = "Given configuration has been successfully added for the selected month.";
} else {
Optional<ConfigValue> configValueOpt = configurations.stream().filter(configValueData -> {
return configValueData.getConfigName().equals(value.getConfigName());
}).findFirst();
if (configValueOpt.isPresent()) {
result = "Configuration with given name already exists for the selected month.";
} else {
configurations.add(value);
currentConfigurations.put(yearMonth, configurations);
result = "Given configuration has been successfully added for the selected month.";
}
}
}
LOGGER.debug("STOP : addConfiguration(String, ConfigValue) of " + this.getClass().getSimpleName());
} catch (Exception e) {
LOGGER.error("EXCEPTION : addConfiguration(String, ConfigValue) of " + this.getClass().getSimpleName());
}
return result;
}

public void removeAllConfigurationsForYearMonth(String yearMonth) {
/**
* To remove all the configuration for the given month year.
*
* @param yearMonth year month like 0520119 for May 2019.
* @return result
*/
public String removeAllConfigurationsForYearMonth(String yearMonth) {
String result = "Oops! Something went wrong while deleting the configurations for the selected month.";
try {
LOGGER.debug("START : removeAllConfigurationsForYearMonth(String) of " + this.getClass().getSimpleName());
if (currentConfigurations != null && yearMonth != null && yearMonth.trim().length() > 0) {
List<ConfigValue> configValues = currentConfigurations.remove(yearMonth);
if (configValues != null && configValues.size() > 0) {
result = "All the configurtions for the selected month got deleted successfully.";
} else {
result = "Selected month don't have any configurtions asscoiated with it.";
}
}
LOGGER.debug("STOP : removeAllConfigurationsForYearMonth(String) of " + this.getClass().getSimpleName());
} catch (Exception e) {
LOGGER.error(
"EXCEPTION : removeAllConfigurationsForYearMonth(String) of " + this.getClass().getSimpleName());
}
return result;
}

}
/**
* To remove given configuration for the given month year based on config id.
*
* @param yearMonth year month like 0520119 for May 2019.
* @param value Config Value to be added.
*/
public String removeOneConfigForYearMonth(String yearMonth, Integer configId) {
boolean result = false;
String response = "Oops! Something went wrong while deleting the selected configuration for the selected month.";
try {
LOGGER.debug(
"START : removeOneConfigForYearMonth(String, ConfigValue) of " + this.getClass().getSimpleName());
if (currentConfigurations != null && yearMonth != null && yearMonth.trim().length() > 0 && configId != null
&& configId >= 0) {
List<ConfigValue> configurations = currentConfigurations.get(yearMonth);
if (configurations != null) {
for (int i = 0; i < configurations.size(); i++) {
ConfigValue listConfigValue = configurations.get(i);
if (listConfigValue.getConfigId() == configId) {
configurations.remove(i);
result = true;
break;
}
}
if (result == true) {
response = "Successfully deleted the selected configuration for the selected month.";
} else {
response = "The selected configuration is not found for the selected month.";
}
}
}
LOGGER.debug(
"STOP : removeOneConfigForYearMonth(String, ConfigValue) of " + this.getClass().getSimpleName());
} catch (Exception e) {
LOGGER.error("EXCEPTION : removeOneConfigForYearMonth(String, ConfigValue) of "
+ this.getClass().getSimpleName());
}
return response;
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,63 @@
package com.att.data.configurations;

/**
* Data Model
* Data Model - Configuration Value
*/
public class ConfigValue {
private String configName;
private int configId;

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

public ConfigValue() {

}

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

public String getConfigName() {
return this.configName;
}

public void setConfigId(int id) {
this.configId = id;
}

public int getConfigId() {
return this.configId;
}
private String configName;
private int configId;

/**
* Constructor initialization with all properties.
*
* @param name Configuration name
* @param id Configuration id
*/
public ConfigValue(String name, int id) {
this.configId = id;
this.configName = name;
}

/**
* Default constructor.
*/
public ConfigValue() {

}

/**
* To set configuration name
*
* @param name configuration name
*/
public void setConfigName(String name) {
this.configName = name;
}

/**
* To get configuration name
*
* @return configuration name
*/
public String getConfigName() {
return this.configName;
}

/**
* To set configuration id.
*
* @param name configuration id
*/
public void setConfigId(int id) {
this.configId = id;
}

/**
* To get configuration id
*
* @return configuration id
*/
public int getConfigId() {
return this.configId;
}
}
Loading