Skip to content
Snippets Groups Projects
Commit ad3c6fd2 authored by gakin's avatar gakin
Browse files

SSDMSSDM-2898 : Automatically created sample codes should form a continuous...

SSDMSSDM-2898 : Automatically created sample codes should form a continuous sequence (working  prototype)

SVN: 35356
parent 0a1df03a
No related branches found
No related tags found
No related merge requests found
...@@ -22,8 +22,11 @@ import java.util.List; ...@@ -22,8 +22,11 @@ import java.util.List;
import net.lemnik.eodsql.BaseQuery; import net.lemnik.eodsql.BaseQuery;
import net.lemnik.eodsql.QueryTool; import net.lemnik.eodsql.QueryTool;
import net.lemnik.eodsql.Select; import net.lemnik.eodsql.Select;
import ch.systemsx.cisd.common.properties.PropertyUtils;
import ch.systemsx.cisd.common.spring.ExposablePropertyPlaceholderConfigurer;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDAOFactory; import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDAOFactory;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.DAOFactory;
import ch.systemsx.cisd.openbis.generic.shared.Constants;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.EntityKind; import ch.systemsx.cisd.openbis.generic.shared.basic.dto.EntityKind;
import ch.systemsx.cisd.openbis.generic.shared.dto.TableNames; import ch.systemsx.cisd.openbis.generic.shared.dto.TableNames;
...@@ -38,10 +41,15 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.TableNames; ...@@ -38,10 +41,15 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.TableNames;
*/ */
public class EntityCodeGenerator public class EntityCodeGenerator
{ {
private final IDAOFactory daoFactory; private final IDAOFactory daoFactory;
private final CountQuery countQuery; private final CountQuery countQuery;
private final MaxQuery maxQuery;
private boolean isCreateContinuousSampleCodes;
public static interface CountQuery extends BaseQuery public static interface CountQuery extends BaseQuery
{ {
public final static String PREFIX = "SELECT count(*) FROM "; public final static String PREFIX = "SELECT count(*) FROM ";
...@@ -61,10 +69,27 @@ public class EntityCodeGenerator ...@@ -61,10 +69,27 @@ public class EntityCodeGenerator
public int getMaterialsCount(String code); public int getMaterialsCount(String code);
} }
public static interface MaxQuery extends BaseQuery
{
@Select(sql = "SELECT max(substr(code, length(?{1})+1)::int) "
+ "FROM samples WHERE code similar to ?{1} || '[1234567890]+'")
public int getMaxCode(String prefix);
}
public EntityCodeGenerator(IDAOFactory daoFactory) public EntityCodeGenerator(IDAOFactory daoFactory)
{ {
this.daoFactory = daoFactory; this.daoFactory = daoFactory;
if (daoFactory instanceof DAOFactory)
{
ExposablePropertyPlaceholderConfigurer configurer = ((DAOFactory) daoFactory).getExposablePropertyPlaceholderConfigurer();
this.isCreateContinuousSampleCodes =
PropertyUtils.getBoolean(configurer.getResolvedProps(), Constants.CREATE_CONTINUOUS_SAMPLES_CODES_KEY, false);
} else
{
this.isCreateContinuousSampleCodes = false;
}
this.countQuery = QueryTool.getManagedQuery(CountQuery.class); this.countQuery = QueryTool.getManagedQuery(CountQuery.class);
this.maxQuery = QueryTool.getManagedQuery(MaxQuery.class);
} }
public String generateCode(String codePrefix, EntityKind entityKind) public String generateCode(String codePrefix, EntityKind entityKind)
...@@ -82,6 +107,38 @@ public class EntityCodeGenerator ...@@ -82,6 +107,38 @@ public class EntityCodeGenerator
* @param numberOfCodes Number of codes to be generated. * @param numberOfCodes Number of codes to be generated.
*/ */
public List<String> generateCodes(String codePrefix, EntityKind entityKind, int numberOfCodes) public List<String> generateCodes(String codePrefix, EntityKind entityKind, int numberOfCodes)
{
if (isCreateContinuousSampleCodes && entityKind == EntityKind.SAMPLE)
{
return generateContinuousSampleCodes(codePrefix, entityKind, numberOfCodes);
}
else
{
return generateCodesFromSequence(codePrefix, entityKind, numberOfCodes);
}
}
private List<String> generateContinuousSampleCodes(String codePrefix, EntityKind entityKind, int numberOfCodes)
{
int maxCode = maxQuery.getMaxCode(codePrefix);
final String[] codes = new String[numberOfCodes];
for (int i = 0; i < numberOfCodes; i++)
{
String code;
do
{
maxCode++;
code = codePrefix + maxCode;
} while (isCodeUsed(code, entityKind));
codes[i] = code;
}
return Arrays.asList(codes);
}
private List<String> generateCodesFromSequence(String codePrefix, EntityKind entityKind, int numberOfCodes)
{ {
final String[] codes = new String[numberOfCodes]; final String[] codes = new String[numberOfCodes];
for (int i = 0; i < numberOfCodes; i++) for (int i = 0; i < numberOfCodes; i++)
...@@ -125,5 +182,4 @@ public class EntityCodeGenerator ...@@ -125,5 +182,4 @@ public class EntityCodeGenerator
return count > 0; return count > 0;
} }
} }
...@@ -371,4 +371,9 @@ public final class DAOFactory extends AuthorizationDAOFactory implements IDAOFac ...@@ -371,4 +371,9 @@ public final class DAOFactory extends AuthorizationDAOFactory implements IDAOFac
} }
} }
public ExposablePropertyPlaceholderConfigurer getExposablePropertyPlaceholderConfigurer()
{
return configurer;
}
} }
...@@ -34,4 +34,6 @@ public class Constants ...@@ -34,4 +34,6 @@ public class Constants
public static final int DEFAULT_SPEED_HINT = -MAX_SPEED / 2; public static final int DEFAULT_SPEED_HINT = -MAX_SPEED / 2;
public static final String PROJECT_SAMPLES_ENABLED_KEY = "project-samples-enabled"; public static final String PROJECT_SAMPLES_ENABLED_KEY = "project-samples-enabled";
public static final String CREATE_CONTINUOUS_SAMPLES_CODES_KEY = "create-continuous-sample-codes";
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment