Skip to content
Snippets Groups Projects
Commit 026e1871 authored by Raphael GIRARDOT's avatar Raphael GIRARDOT
Browse files

- removed duplicates

- better programming practices
parent 737866e9
No related branches found
No related tags found
No related merge requests found
package fr.soleil.archiving.hdbtdb.api.management.attributes.tdb.delete;
import fr.soleil.archiving.common.api.ConnectionFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.soleil.archiving.common.api.exception.ArchivingException;
import fr.soleil.archiving.hdbtdb.api.ConfigConst;
import fr.soleil.archiving.hdbtdb.api.DataBaseManager;
import fr.soleil.archiving.hdbtdb.api.TDBDataBaseManager;
import fr.soleil.archiving.hdbtdb.api.management.database.commands.ConnectionCommands;
import fr.soleil.archiving.hdbtdb.api.manager.ArchivingManagerApiRefFactory;
import fr.soleil.archiving.hdbtdb.api.manager.IArchivingManagerApiRef;
import fr.soleil.archiving.hdbtdb.api.utils.database.DbUtilsFactory;
import fr.soleil.archiving.hdbtdb.api.utils.database.IDbUtils;
import fr.soleil.database.connection.AbstractDataBaseConnector;
import fr.soleil.database.connection.DataBaseParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
public class TdbAttributeDelete {
private final Logger logger = LoggerFactory.getLogger(TdbAttributeDelete.class);
......@@ -45,8 +38,8 @@ public class TdbAttributeDelete {
conn = connector.getConnection();
final String name = attributeList[i];
final String deleteString = new StringBuilder("DELETE FROM ").append(connector.getSchema())
.append(".").append(dbUtils.getTableName(name)).append(" WHERE ")
.append(ConfigConst.TIME).append(" <= ?").toString();
.append(".").append(dbUtils.getTableName(name)).append(" WHERE ").append(ConfigConst.TIME)
.append(" <= ?").toString();
final String truncateString = new StringBuilder("TRUNCATE TABLE ").append(connector.getSchema())
.append(".").append(dbUtils.getTableName(name)).toString();
boolean everythingIsOld = false;
......
......@@ -416,75 +416,6 @@ public abstract class GenericMode implements IGenericMode, DBExtractionConst {
return result;
}
/**
* <b>Description : </b>
*
* @param attributeName
* The name of the attribute
* @return a String : The name of the corresponding archiver if the
* attribute is beeing archived, an empty String otherwise
* @throws ArchivingException
*/
@Override
public String getArchiverForAttributeEvenIfTheStopDateIsNotNull(final String attributeName)
throws ArchivingException {
String result;
if (connector == null) {
result = null;
} else {
final List<String> archivVect = new ArrayList<String>();
int res = 0;
Connection conn = null;
PreparedStatement preparedStatement = null;
ResultSet rset = null;
// Build the query string
final StringBuilder selectField = new StringBuilder("SELECT ").append(ConfigConst.AMT).append(DB_SEPARATOR)
.append(ConfigConst.ARCHIVER);
selectField.append(" FROM ").append(connector.getSchema()).append(DB_SEPARATOR).append(ConfigConst.AMT)
.append(VALUE_SEPARATOR).append(connector.getSchema()).append(DB_SEPARATOR).append(ConfigConst.ADT)
.append(" WHERE ((").append(ConfigConst.AMT_ID).append(" = ").append(ConfigConst.ADT)
.append(DB_SEPARATOR).append(ConfigConst.ID).append(") AND ");
if (caseSensitiveDb) {
// XXX Use "LOWER" to avoid case sensitivity problems. Might reduce performances.
selectField.append("(LOWER(").append(ConfigConst.ADT).append(DB_SEPARATOR).append(ConfigConst.FULL_NAME)
.append(") = LOWER(?))");
} else {
selectField.append("(").append(ConfigConst.ADT).append(DB_SEPARATOR).append(ConfigConst.FULL_NAME)
.append(" = ?)");
}
selectField.append(" AND (").append(ConfigConst.AMT_STOP_DATE).append(" IS NULL ))");
String isArchivedQuery = selectField.toString();
try {
conn = connector.getConnection();
if (conn != null) {
preparedStatement = conn.prepareStatement(isArchivedQuery);
final String attrName = attributeName.trim();
isArchivedQuery = isArchivedQuery.replaceFirst("\\?", attrName);
preparedStatement.setString(1, attrName);
rset = preparedStatement.executeQuery();
while (rset.next()) {
archivVect.add(rset.getString(1));
res++;
}
}
} catch (final SQLException e) {
throw new ArchivingException(e, isArchivedQuery);
} finally {
ConnectionCommands.close(rset);
ConnectionCommands.close(preparedStatement);
connector.closeConnection(conn);
}
// Close the connection with the database
if (res > 0) {
result = archivVect.get(0);
} else {
result = "";
}
}
return result;
}
/**
* <b>Description : </b> Gets the current archiving mode for a given
* attribute name.
......
......@@ -21,8 +21,6 @@ public interface IGenericMode {
String getArchiverForAttribute(String attributeName) throws ArchivingException;
String getArchiverForAttributeEvenIfTheStopDateIsNotNull(String attributeName) throws ArchivingException;
Mode getCurrentArchivingMode(String attributeName) throws ArchivingException;
Collection<AttributeLightMode> getArchiverCurrentTasks(String archiverName) throws ArchivingException;
......
......@@ -26,8 +26,9 @@
package fr.soleil.archiving.hdbtdb.api.tools;
import java.awt.Color;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import fr.esrf.Tango.DevFailed;
import fr.esrf.TangoApi.DbDatum;
......@@ -36,15 +37,15 @@ import fr.soleil.archiving.common.api.exception.ArchivingException;
public final class Archiver {
private String name;
private boolean isDedicated;
private boolean dedicated;
private String[] reservedAttributes;
private boolean isLoaded = false;
private boolean isHistoric = false;
private boolean isExported;
private boolean loaded = false;
private boolean historic = false;
private boolean exported;
private static Map<String, Archiver> attributesToHdbDedicatedArchiver;
private static Map<String, Archiver> attributesToTdbDedicatedArchiver;
private final static Map<String, Archiver> archivers = new HashMap<String, Archiver>();
private static final Map<String, Archiver> ATTRIBUTES_TO_HDB_DEDICATED_ARCHIVERS = new ConcurrentHashMap<>();
private static final Map<String, Archiver> ATTRIBUTES_TO_TDB_DEDICATED_ARCHIVERS = new ConcurrentHashMap<>();
private static final Map<String, Archiver> ARCHIVERS = new ConcurrentHashMap<>();
public static final int NON_DEDICATED_ARCHIVER_RIGHT = 1;
public static final int DEDICATED_ARCHIVER_RIGHT = 2;
......@@ -54,23 +55,28 @@ public final class Archiver {
private static final String RESERVED_ATTRIBUTES = "reservedAttributes";
private static final String IS_DEDICATED = "isDedicated";
private static final Color DARKER_GREEN = new Color(0, 170, 0);
private static final Color DARKER_RED = new Color(200, 0, 0);
public static final Color DARKER_GREEN = new Color(0, 170, 0);
public static final Color DARKER_RED = new Color(200, 0, 0);
public Archiver(final String name, final boolean historic) {
this.name = name;
this.historic = historic;
}
public void load() throws ArchivingException {
if (!this.isLoaded) {
if (!loaded) {
try {
final Archiver loaded = getArchiver(this.name, this.isHistoric);
this.setDedicated(loaded.isDedicated());
this.setReservedAttributes(loaded.getReservedAttributes());
final Archiver loaded = getArchiver(name, historic);
setDedicated(loaded.isDedicated());
setReservedAttributes(loaded.getReservedAttributes());
} finally {
this.isLoaded = true;
loaded = true;
}
}
}
public Color getAssociationColor(final String attributeName) throws ArchivingException {
final int colorCase = this.hasReservedAttribute(attributeName);
final int colorCase = hasReservedAttribute(attributeName);
Color color;
switch (colorCase) {
case Archiver.DEDICATED_ARCHIVER_RIGHT:
......@@ -88,21 +94,18 @@ public final class Archiver {
}
private int hasReservedAttribute(final String attributeName) throws ArchivingException {
this.load();
if (!this.isDedicated) {
return nonDedicated(attributeName);
}
if (this.reservedAttributes == null || this.reservedAttributes.length == 0) {
return nonDedicated(attributeName);
}
if (attributeName == null || attributeName.equals("")) {
return NON_DEDICATED_ARCHIVER_RIGHT;
}
load();
final int ret;
if (!dedicated) {
ret = nonDedicated(attributeName);
} else if (reservedAttributes == null || reservedAttributes.length == 0) {
ret = nonDedicated(attributeName);
} else if (attributeName == null || attributeName.isEmpty()) {
ret = NON_DEDICATED_ARCHIVER_RIGHT;
} else {
boolean hasAttribute = false;
for (int i = 0; i < this.reservedAttributes.length; i++) {
final String nextAttribute = this.reservedAttributes[i];
for (int i = 0; i < reservedAttributes.length; i++) {
final String nextAttribute = reservedAttributes[i];
if (nextAttribute == null) {
continue;
}
......@@ -112,35 +115,30 @@ public final class Archiver {
break;
}
}
final int ret = hasAttribute ? DEDICATED_ARCHIVER_RIGHT : DEDICATED_ARCHIVER_WRONG;
ret = hasAttribute ? DEDICATED_ARCHIVER_RIGHT : DEDICATED_ARCHIVER_WRONG;
}
return ret;
}
private int nonDedicated(final String attributeName) {
final Map<String, Archiver> attributesToDedicatedArchiver = this.isHistoric ? attributesToHdbDedicatedArchiver
: attributesToTdbDedicatedArchiver;
final Map<String, Archiver> attributesToDedicatedArchiver = historic ? ATTRIBUTES_TO_HDB_DEDICATED_ARCHIVERS
: ATTRIBUTES_TO_TDB_DEDICATED_ARCHIVERS;
final int ret;
if (attributesToDedicatedArchiver == null || attributeName == null) {
return NON_DEDICATED_ARCHIVER_RIGHT;
}
if (!attributesToDedicatedArchiver.containsKey(attributeName)) {
return NON_DEDICATED_ARCHIVER_RIGHT;
ret = NON_DEDICATED_ARCHIVER_RIGHT;
} else if (!attributesToDedicatedArchiver.containsKey(attributeName)) {
ret = NON_DEDICATED_ARCHIVER_RIGHT;
} else {
return NON_DEDICATED_ARCHIVER_WRONG;
}
ret = NON_DEDICATED_ARCHIVER_WRONG;
}
public Archiver(final String name, final boolean isHistoric) {
this.name = name;
this.isHistoric = isHistoric;
return ret;
}
/**
* @return Returns the name.
*/
public String getName() {
return this.name;
return name;
}
/**
......@@ -155,22 +153,21 @@ public final class Archiver {
* @return Returns the isDedicated.
*/
public boolean isDedicated() {
return this.isDedicated;
return dedicated;
}
/**
* @param isDedicated
* The isDedicated to set.
* @param dedicated The isDedicated to set.
*/
public void setDedicated(final boolean isDedicated) {
this.isDedicated = isDedicated;
public void setDedicated(final boolean dedicated) {
this.dedicated = dedicated;
}
/**
* @return Returns the reservedAttributes.
*/
public String[] getReservedAttributes() {
return this.reservedAttributes;
return reservedAttributes;
}
/**
......@@ -181,6 +178,30 @@ public final class Archiver {
this.reservedAttributes = reservedAttributes;
}
public void setExported(final boolean exported) {
this.exported = exported;
}
/**
* @return Returns the isExported.
*/
public boolean isExported() {
return exported;
}
protected static void updateMap(Map<String, Archiver> source, Map<String, Archiver> dest) {
dest.clear();
if (source != null) {
for (Entry<String, Archiver> entry : source.entrySet()) {
String attribute = entry.getKey();
Archiver archiver = entry.getValue();
if ((attribute != null) && (archiver != null)) {
dest.put(attribute, archiver);
}
}
}
}
/**
* @param attributesToHdbDedicatedArchiver
* The deviceDefinedAttributesToDedicatedArchiver to set.
......@@ -188,14 +209,14 @@ public final class Archiver {
public static void setAttributesToDedicatedArchiver(final boolean historic,
final Map<String, Archiver> deviceDefinedAttributesToDedicatedArchiver) {
if (historic) {
attributesToHdbDedicatedArchiver = deviceDefinedAttributesToDedicatedArchiver;
updateMap(deviceDefinedAttributesToDedicatedArchiver, ATTRIBUTES_TO_HDB_DEDICATED_ARCHIVERS);
} else {
attributesToTdbDedicatedArchiver = deviceDefinedAttributesToDedicatedArchiver;
updateMap(deviceDefinedAttributesToDedicatedArchiver, ATTRIBUTES_TO_TDB_DEDICATED_ARCHIVERS);
}
}
private static Archiver getArchiver(final String my_archiver, final boolean _historic) throws ArchivingException {
final Archiver ret = new Archiver(my_archiver, _historic);
private static Archiver getArchiver(final String my_archiver, final boolean historic) throws ArchivingException {
final Archiver ret = new Archiver(my_archiver, historic);
DeviceProxy archiverProxy = null;
try {
......@@ -221,45 +242,33 @@ public final class Archiver {
ret.setDedicated(isDedicated);
ret.setReservedAttributes(reservedAttributes);
archivers.put(ret.getName(), ret);
ARCHIVERS.put(ret.getName(), ret);
} catch (final DevFailed devFailed) {
throw new ArchivingException(devFailed);
}
return ret;
}
public static Archiver findArchiver(final String name, final boolean _historic) throws ArchivingException {
public static Archiver findArchiver(final String name, final boolean historic) throws ArchivingException {
final Archiver ret;
if (name == null) {
return null;
}
final Archiver ret = archivers.get(name);
if (ret == null) {
return getArchiver(name, _historic);
ret = null;
} else {
return ret;
final Archiver archiver = ARCHIVERS.get(name);
if (archiver == null) {
ret = getArchiver(name, historic);
} else {
ret = archiver;
}
}
return ret;
}
/**
* @return Returns the attributesToDedicatedArchiver.
*/
public static Map<String, Archiver> getAttributesToDedicatedArchiver(final boolean historic) {
if (historic) {
return attributesToHdbDedicatedArchiver;
} else {
return attributesToTdbDedicatedArchiver;
}
}
public void setExported(final boolean _isExported) {
this.isExported = _isExported;
return historic ? ATTRIBUTES_TO_HDB_DEDICATED_ARCHIVERS : ATTRIBUTES_TO_TDB_DEDICATED_ARCHIVERS;
}
/**
* @return Returns the isExported.
*/
public boolean isExported() {
return this.isExported;
}
}
......@@ -70,19 +70,19 @@ import org.slf4j.LoggerFactory;
public class ArchivingMessConfig {
Logger logger = LoggerFactory.getLogger(ArchivingMessConfig.class);
private static final Logger LOGGER = LoggerFactory.getLogger(ArchivingMessConfig.class);
// private String device_in_charge = null;
private boolean grouped = false;
private Map<String, AttributeLightMode> attributeList = new HashMap<String, AttributeLightMode>();
private Map<String, String> attributeToDedicatedArchiverHasthable = new HashMap<String, String>();
private Map<String, AttributeLightMode> attributeList = new HashMap<>();
private Map<String, String> attributeToDedicatedArchiverHasthable = new HashMap<>();
/**
* Create an object with the simple contructor
*
* @return
*/
static public ArchivingMessConfig basicObjectCreation() {
public static ArchivingMessConfig basicObjectCreation() {
return new ArchivingMessConfig();
}
......@@ -178,7 +178,7 @@ public class ArchivingMessConfig {
AttributeLightMode.creationWithoutFullInformation(attributeLightModeArray));
}
}
logger.debug("adding attributes {}", attributeList);
LOGGER.debug("adding attributes {}", attributeList);
}
public boolean isGrouped() {
......@@ -234,7 +234,6 @@ public class ArchivingMessConfig {
return attributeLightMode;
}
public void add(final AttributeLightMode attributeLightMode) {
attributeList.put(attributeLightMode.getAttributeCompleteName(), attributeLightMode);
}
......
......@@ -20,6 +20,7 @@ import fr.soleil.archiving.hdbtdb.api.tools.mode.ModeDifference;
import fr.soleil.archiving.hdbtdb.api.tools.mode.ModePeriode;
import fr.soleil.archiving.hdbtdb.api.tools.mode.ModeRelatif;
import fr.soleil.archiving.hdbtdb.api.tools.mode.ModeSeuil;
import fr.soleil.lib.project.ObjectUtils;
public class HdbModeTest extends OracleConnectorTest {
......@@ -45,7 +46,7 @@ public class HdbModeTest extends OracleConnectorTest {
public void getDeviceInChargeTest() throws ArchivingException {
HdbMode mode = new HdbMode(hdbConnector);
assertEquals("archiving/hdb-oracle/hdbarchiver.01", mode.getDeviceInCharge(DEFAULT + "double_scalar_rw"));
assertEquals("", mode.getDeviceInCharge(DEFAULT + "double_scalar_ro"));
assertEquals(ObjectUtils.EMPTY_STRING, mode.getDeviceInCharge(DEFAULT + "double_scalar_ro"));
}
@Test
......@@ -60,15 +61,7 @@ public class HdbModeTest extends OracleConnectorTest {
public void getArchiverForAttributeTest() throws ArchivingException {
HdbMode mode = new HdbMode(hdbConnector);
assertEquals("archiving/hdb-oracle/hdbarchiver.01", mode.getArchiverForAttribute(DEFAULT + "double_scalar_rw"));
assertEquals("", mode.getArchiverForAttribute(DEFAULT + "double_scalar_ro"));
}
@Test
public void getArchiverForAttributeEvenIfTheStopDateIsNotNullTest() throws ArchivingException {
HdbMode mode = new HdbMode(hdbConnector);
assertEquals("archiving/hdb-oracle/hdbarchiver.01",
mode.getArchiverForAttributeEvenIfTheStopDateIsNotNull(DEFAULT + "double_scalar_rw"));
assertEquals("", mode.getArchiverForAttributeEvenIfTheStopDateIsNotNull(DEFAULT + "double_scalar_ro"));
assertEquals(ObjectUtils.EMPTY_STRING, mode.getArchiverForAttribute(DEFAULT + "double_scalar_ro"));
}
@Test
......@@ -237,7 +230,6 @@ public class HdbModeTest extends OracleConnectorTest {
assertNull(currentMode.getModeR());
assertNull(currentMode.getModeT());
list = mode.getArchiverCurrentTasks(newArchiverName);
assertEquals(1, list.size());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment