Skip to content
Snippets Groups Projects
Commit b3ae87f9 authored by Gwenaelle ABEILLE's avatar Gwenaelle ABEILLE
Browse files

refactoring to manage class property "shortPeriodAttributes" from the...

refactoring to manage class property "shortPeriodAttributes" from the HdbArchiver device instead of the API
parent 1a5101e7
No related branches found
No related tags found
No related merge requests found
package fr.soleil.archiving.hdbtdb.api.tools.mode;
import fr.soleil.archiving.common.api.exception.ArchivingException;
import java.util.HashMap;
import java.util.Map;
import fr.esrf.Tango.ErrSeverity;
import fr.soleil.archiving.common.api.exception.ArchivingException;
import fr.soleil.archiving.common.api.tools.GlobalConst;
import fr.soleil.archiving.hdbtdb.api.ConfigConst;
import fr.soleil.archiving.hdbtdb.api.GetArchivingConf;
/**
* Handles comparisons between the initialized list of short period attributes,
* and other attributes
......@@ -17,105 +13,11 @@ import fr.soleil.archiving.hdbtdb.api.GetArchivingConf;
*/
public class ShortPeriodAttributesManager {
private static final String SHORT_PERIOD_ATTRIBUTE_PROPERTY = "shortPeriodAttributes";
private static final String SEPARATOR = ",";
private static final String DEFAULT_INIT_LIST_REASON = "Failed while executing ShortPeriodAttributeManager.initShortPeriodAttributes()...";
/**
* Map containing short period attributes: key: the full name of the
* attribute value: the period in seconds
*/
private static Map<String, Integer> shortPeriodAttributes;
/**
* Initializes the shortPeriodAttributes map with the contents of the
* property 'shortPeriodAttributes' in the HdbArchiver device class. It
* stops the launch of HdbArchiver device when the short attributes map is
* not formatted correctly : period that isn't between 0 and 10 excluded, or
* syntax error
*/
private static void initShortPeriodAttributes() throws ArchivingException {
shortPeriodAttributes = new HashMap<String, Integer>();
String cause, message, desc;
final String[] shortPeriodAttributesArray = GetArchivingConf.readStringArrayInDB(ConfigConst.HDB_CLASS_DEVICE,
SHORT_PERIOD_ATTRIBUTE_PROPERTY);
if (shortPeriodAttributesArray != null) {
for (final String attribute : shortPeriodAttributesArray) {
if (attribute != null && !attribute.trim().isEmpty()) {
String temp = attribute;
int delimiterCount = 0;
while (temp.indexOf(',') > -1) {
// Doing so, we are sure to test all bad cases, even
// the ones not detectable by split
delimiterCount++;
if (delimiterCount > 1) {
temp = null;
// property is not well formatted (too many
// separators)
throw generateDefaultShortPeriodAttributeException();
}
temp = temp.replaceFirst(",", "");
}
temp = null;
final String[] attributeParts = attribute.split(SEPARATOR);
if (attributeParts.length > 2) {
// property is not well formatted (too many
// separators)
throw generateDefaultShortPeriodAttributeException();
} else {
final String attributeName = attributeParts[0].trim().toLowerCase();
if (attributeName.isEmpty()) {
cause = "INVALID_NAME";
message = GlobalConst.ARCHIVING_ERROR_PREFIX + cause;
desc = "Some attributes in the short period attributes list have an invalid name (should not be empty)";
throw new ArchivingException(message, DEFAULT_INIT_LIST_REASON, ErrSeverity.ERR, desc, "");
} else if (shortPeriodAttributes.containsKey(attributeName)) {
cause = "DUPLICATED_NAME";
message = GlobalConst.ARCHIVING_ERROR_PREFIX + cause;
desc = "Some attributes appear at least twice in the short period attributes list";
throw new ArchivingException(message, DEFAULT_INIT_LIST_REASON, ErrSeverity.ERR, desc, "");
} else {
final Integer attributePeriod = Integer.parseInt(attributeParts[1]);
if (attributePeriod > 0 && attributePeriod < 10) {
shortPeriodAttributes.put(attributeName, attributePeriod);
} else {
cause = "WRONG_PERIOD_VALUE";
message = GlobalConst.ARCHIVING_ERROR_PREFIX + cause;
desc = "Some attributes in the short period attributes list have a wrong period value (should be between 0 and 10 excluded)";
throw new ArchivingException(message, DEFAULT_INIT_LIST_REASON, ErrSeverity.ERR, desc,
"");
}
}
}
}
}
}
}
private static ArchivingException generateDefaultShortPeriodAttributeException() {
return new ArchivingException(GlobalConst.ARCHIVING_ERROR_PREFIX + "WRONG_FORMAT", DEFAULT_INIT_LIST_REASON,
ErrSeverity.ERR, "The shortPeriodAttributes property is not correctly formatted", "");
}
/**
* Gets the current instance of the map, and initializes it if it's null
*/
public static Map<String, Integer> getShortPeriodAttributes() throws ArchivingException {
if (shortPeriodAttributes == null) {
initShortPeriodAttributes();
}
return shortPeriodAttributes;
}
/**
* For JUnit purpose only
*/
public static void setShortPeriodAttributes(final Map<String, Integer> shortPeriodAttributes) {
ShortPeriodAttributesManager.shortPeriodAttributes = shortPeriodAttributes;
}
private static Map<String, Integer> shortPeriodAttributes = new HashMap<>();
/**
* Return true if the input attribute name is in the shortPeriodAttributes
......@@ -125,7 +27,7 @@ public class ShortPeriodAttributesManager {
if (attributeName == null) {
return false;
}
return getShortPeriodAttributes().containsKey(attributeName.toLowerCase());
return shortPeriodAttributes.containsKey(attributeName.toLowerCase());
}
/**
......@@ -135,6 +37,14 @@ public class ShortPeriodAttributesManager {
if (attributeName == null) {
return null;
}
return getShortPeriodAttributes().get(attributeName.toLowerCase());
return shortPeriodAttributes.get(attributeName.toLowerCase());
}
public static Map<String, Integer> getShortPeriodAttributes() {
return shortPeriodAttributes;
}
public static void setShortPeriodAttributes(final Map<String, Integer> shortPeriodAttributes) {
ShortPeriodAttributesManager.shortPeriodAttributes = shortPeriodAttributes;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment