diff --git a/src/main/java/org/icatproject/core/DocGenerator.java b/src/main/java/org/icatproject/core/DocGenerator.java
index 8ccf40feb..23eba9099 100644
--- a/src/main/java/org/icatproject/core/DocGenerator.java
+++ b/src/main/java/org/icatproject/core/DocGenerator.java
@@ -17,8 +17,6 @@
public class DocGenerator {
- private static EntityInfoHandler eiHandler = EntityInfoHandler.getInstance();
-
private static Logger logger = LoggerFactory.getLogger(DocGenerator.class);
public static void main(String[] args) throws Exception {
@@ -45,20 +43,18 @@ public static void main(String[] args) throws Exception {
for (String cname : cnames) {
out.print("
" + cname + "
");
- Class> klass = Class.forName("org.icatproject.core.entity." + cname);
- @SuppressWarnings("unchecked")
- Class extends EntityBaseBean> eklass = (Class extends EntityBaseBean>) klass;
- String classComment = eiHandler.getClassComment(eklass);
+ Class extends EntityBaseBean> eklass = EntityInfoHandler.getClass(cname);
+ String classComment = EntityInfoHandler.getClassComment(eklass);
if (classComment == null) {
System.out.println(cname + " has no comment");
} else {
out.println("" + classComment + "
");
}
- Set fields = eiHandler.getGetters(eklass).keySet();
- Map fieldComments = eiHandler.getFieldComments(eklass);
- Set notnullables = new HashSet(eiHandler.getNotNullableFields(eklass));
- Map stringFields = eiHandler.getStringFields(eklass);
+ Set fields = new HashSet(EntityInfoHandler.getGetters(eklass).keySet());
+ Map fieldComments = EntityInfoHandler.getFieldComments(eklass);
+ Set notnullables = new HashSet(EntityInfoHandler.getNotNullableFields(eklass));
+ Map stringFields = EntityInfoHandler.getStringFields(eklass);
Iterator iter = fields.iterator();
while (iter.hasNext()) {
@@ -69,7 +65,7 @@ public static void main(String[] args) throws Exception {
}
}
- List constraint = eiHandler.getConstraintFields(eklass);
+ List constraint = EntityInfoHandler.getConstraintFields(eklass);
if (!constraint.isEmpty()) {
out.print("Uniqueness constraint ");
first = true;
@@ -86,7 +82,7 @@ public static void main(String[] args) throws Exception {
out.println(
"
Relationships
Card | Class | Field | Cascaded | Description |
");
- for (Relationship r : eiHandler.getRelatedEntities(eklass)) {
+ for (Relationship r : EntityInfoHandler.getRelatedEntities(eklass)) {
Field f = r.getField();
boolean notnullable = notnullables.contains(f);
boolean many = r.isCollection();
diff --git a/src/main/java/org/icatproject/core/entity/EntityBaseBean.java b/src/main/java/org/icatproject/core/entity/EntityBaseBean.java
index bb3c8a2cf..e09986f3e 100644
--- a/src/main/java/org/icatproject/core/entity/EntityBaseBean.java
+++ b/src/main/java/org/icatproject/core/entity/EntityBaseBean.java
@@ -40,8 +40,6 @@
@MappedSuperclass
public abstract class EntityBaseBean implements Serializable {
- private static final EntityInfoHandler eiHandler = EntityInfoHandler.getInstance();
-
private static final Logger logger = LoggerFactory.getLogger(EntityBaseBean.class);
@Column(name = "CREATE_ID", nullable = false)
@@ -84,8 +82,8 @@ void addToClone(EntityBaseBean clone) {
public void addToLucene(LuceneManager lucene) throws IcatException {
lucene.addDocument(this);
Class extends EntityBaseBean> klass = this.getClass();
- Set rs = eiHandler.getRelatedEntities(klass);
- Map getters = eiHandler.getGetters(klass);
+ Set rs = EntityInfoHandler.getRelatedEntities(klass);
+ Map getters = EntityInfoHandler.getGetters(klass);
for (Relationship r : rs) {
if (r.isCollection()) {
Method m = getters.get(r.getField());
@@ -147,9 +145,9 @@ public void collectIds(Map> ids, boolean one, int hereVarNum,
ids.get(beanName).add(id);
try {
- Map getters = eiHandler.getGetters(klass);
+ Map getters = EntityInfoHandler.getGetters(klass);
if (one) {
- for (Relationship r : eiHandler.getOnes(klass)) {
+ for (Relationship r : EntityInfoHandler.getOnes(klass)) {
Field att = r.getField();
EntityBaseBean value = allowedOne(r, getters.get(att), gateKeeper, userId, manager);
if (value != null) {
@@ -307,8 +305,8 @@ public void preparePersist(String modId, EntityManager manager, GateKeeper gateK
}
Class extends EntityBaseBean> klass = this.getClass();
- Set rs = eiHandler.getRelatedEntities(klass);
- Map getters = eiHandler.getGetters(klass);
+ Set rs = EntityInfoHandler.getRelatedEntities(klass);
+ Map getters = EntityInfoHandler.getGetters(klass);
for (Relationship r : rs) {
if (r.isCollection()) {
Method m = getters.get(r.getField());
@@ -342,16 +340,16 @@ public EntityBaseBean pruned(boolean one, int hereVarNum, List steps, long
}
}
try {
- Constructor extends EntityBaseBean> con = eiHandler.getConstructor(klass);
+ Constructor extends EntityBaseBean> con = EntityInfoHandler.getConstructor(klass);
EntityBaseBean clone = con.newInstance();
clone.id = this.id;
clone.createTime = this.createTime;
clone.createId = this.createId;
clone.modTime = this.modTime;
clone.modId = this.modId;
- Set atts = eiHandler.getAttributes(klass);
- Map getters = eiHandler.getGetters(klass);
- Map setters = eiHandler.getSettersForUpdate(klass);
+ Set atts = EntityInfoHandler.getAttributes(klass);
+ Map getters = EntityInfoHandler.getGetters(klass);
+ Map setters = EntityInfoHandler.getSettersForUpdate(klass);
for (Field att : atts) {
Object value = getters.get(att).invoke(this);
if (value != null) {
@@ -359,7 +357,7 @@ public EntityBaseBean pruned(boolean one, int hereVarNum, List steps, long
}
}
if (one) {
- for (Relationship r : eiHandler.getOnes(klass)) {
+ for (Relationship r : EntityInfoHandler.getOnes(klass)) {
Field att = r.getField();
EntityBaseBean value = allowedOne(r, getters.get(att), gateKeeper, userId, manager);
if (value != null) {
diff --git a/src/main/java/org/icatproject/core/entity/PublicStep.java b/src/main/java/org/icatproject/core/entity/PublicStep.java
index 581f11aea..01f02dda6 100644
--- a/src/main/java/org/icatproject/core/entity/PublicStep.java
+++ b/src/main/java/org/icatproject/core/entity/PublicStep.java
@@ -32,8 +32,6 @@ public class PublicStep extends EntityBaseBean implements Serializable {
public static final String GET_ALL_QUERY = "AllowedStep.GetAllQuery";
private static final Logger logger = LoggerFactory.getLogger(PublicStep.class);
- private static final EntityInfoHandler eiHandler = EntityInfoHandler.getInstance();
-
public String getOrigin() {
return origin;
}
@@ -64,7 +62,7 @@ public PublicStep() {
private void fixup(EntityManager manager, GateKeeper gateKeeper) throws IcatException {
Class extends EntityBaseBean> bean = EntityInfoHandler.getClass(origin);
- Set rs = eiHandler.getRelatedEntities(bean);
+ Set rs = EntityInfoHandler.getRelatedEntities(bean);
boolean found = false;
for (Relationship r : rs) {
if (r.getField().getName().equals(field)) {
diff --git a/src/main/java/org/icatproject/core/manager/EntityBeanManager.java b/src/main/java/org/icatproject/core/manager/EntityBeanManager.java
index 1145be30e..185fba507 100644
--- a/src/main/java/org/icatproject/core/manager/EntityBeanManager.java
+++ b/src/main/java/org/icatproject/core/manager/EntityBeanManager.java
@@ -57,7 +57,6 @@
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.StreamingOutput;
-import org.icatproject.core.Constants;
import org.icatproject.core.IcatException;
import org.icatproject.core.IcatException.IcatExceptionType;
import org.icatproject.core.entity.Datafile;
@@ -116,8 +115,6 @@ public enum PersistMode {
private static DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
- private static EntityInfoHandler eiHandler = EntityInfoHandler.getInstance();
-
private static final Logger logger = LoggerFactory.getLogger(EntityBeanManager.class);
private static final Pattern timestampPattern = Pattern.compile(":ts(\\d{14})");
@@ -157,15 +154,15 @@ public enum PersistMode {
private String buildKey(EntityBaseBean bean, Map> exportCaches)
throws IcatException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class extends EntityBaseBean> klass = bean.getClass();
- List constraintFields = eiHandler.getConstraintFields(klass);
+ List constraintFields = EntityInfoHandler.getConstraintFields(klass);
if (constraintFields.isEmpty()) {
return '"' + bean.getId().toString() + '"';
}
- List fields = eiHandler.getFields(klass);
- Map getters = eiHandler.getGetters(klass);
- Map fieldMap = eiHandler.getFieldsByName(klass);
- Set atts = eiHandler.getAttributes(klass);
- Set updaters = eiHandler.getSettersForUpdate(klass).keySet();
+ List fields = EntityInfoHandler.getFields(klass);
+ Map getters = EntityInfoHandler.getGetters(klass);
+ Map fieldMap = EntityInfoHandler.getFieldsByName(klass);
+ Set atts = EntityInfoHandler.getAttributes(klass);
+ Set updaters = EntityInfoHandler.getSettersForUpdate(klass).keySet();
StringBuilder sb = new StringBuilder();
boolean first = true;
for (Field field : fields) {
@@ -200,8 +197,8 @@ private String buildKey(EntityBaseBean bean, Map> expo
private boolean checkIdentityChange(EntityBaseBean thisBean, EntityBaseBean fromBean) throws IcatException {
Class extends EntityBaseBean> klass = thisBean.getClass();
- Map getters = eiHandler.getGetters(klass);
- for (Field field : eiHandler.getRelInKey(klass)) {
+ Map getters = EntityInfoHandler.getGetters(klass);
+ for (Field field : EntityInfoHandler.getRelInKey(klass)) {
try {
Method m = getters.get(field);
EntityBaseBean newValue = (EntityBaseBean) m.invoke(fromBean, new Object[0]);
@@ -416,9 +413,9 @@ public List createMany(String userId, List beans
* Now look for duplicates within the list of objects provided
*/
Class extends EntityBaseBean> entityClass = bean.getClass();
- Map getters = eiHandler.getGetters(entityClass);
+ Map getters = EntityInfoHandler.getGetters(entityClass);
- List constraint = eiHandler.getConstraintFields(entityClass);
+ List constraint = EntityInfoHandler.getConstraintFields(entityClass);
if (!constraint.isEmpty()) {
for (int i = 0; i < pos; i++) {
boolean diff = false;
@@ -699,7 +696,7 @@ private void exportBean(EntityBaseBean bean, OutputStream output, boolean qcolum
} else {
if (value == null) {
output.write(
- eiHandler.getExportNull((Class extends EntityBaseBean>) field.getType()).getBytes());
+ EntityInfoHandler.getExportNull((Class extends EntityBaseBean>) field.getType()).getBytes());
} else {
long obId = ((EntityBaseBean) value).getId();
String obType = value.getClass().getSimpleName();
@@ -727,20 +724,20 @@ private void exportTable(String beanName, Set ids, OutputStream output,
throws IcatException, IOException, IllegalAccessException, IllegalArgumentException,
InvocationTargetException {
logger.debug("Export " + (ids == null ? "complete" : "partial") + " " + beanName);
- Class klass = EntityInfoHandler.getClass(beanName);
+ Class extends EntityBaseBean> klass = EntityInfoHandler.getClass(beanName);
output.write((linesep).getBytes());
if (allAttributes) {
- output.write(eiHandler.getExportHeaderAll(klass).getBytes());
+ output.write(EntityInfoHandler.getExportHeaderAll(klass).getBytes());
} else {
- output.write(eiHandler.getExportHeader(klass).getBytes());
+ output.write(EntityInfoHandler.getExportHeader(klass).getBytes());
}
output.write((linesep).getBytes());
- List fields = eiHandler.getFields(klass);
- Map getters = eiHandler.getGetters(klass);
- Map fieldMap = eiHandler.getFieldsByName(klass);
- Set atts = eiHandler.getAttributes(klass);
- Set updaters = eiHandler.getSettersForUpdate(klass).keySet();
- boolean qcolumn = eiHandler.getConstraintFields(klass).isEmpty();
+ List fields = EntityInfoHandler.getFields(klass);
+ Map getters = EntityInfoHandler.getGetters(klass);
+ Map fieldMap = EntityInfoHandler.getFieldsByName(klass);
+ Set atts = EntityInfoHandler.getAttributes(klass);
+ Set updaters = EntityInfoHandler.getSettersForUpdate(klass).keySet();
+ boolean qcolumn = EntityInfoHandler.getConstraintFields(klass).isEmpty();
boolean notRootUser = !rootUserNames.contains(userId);
if (ids == null) {
@@ -892,8 +889,8 @@ public EntityBaseBean get(String userId, String query, long primaryKey, EntityMa
private List getDependentBeans(EntityBaseBean bean) throws IcatException {
Class extends EntityBaseBean> klass = bean.getClass();
- Set rs = eiHandler.getRelatedEntities(klass);
- Map getters = eiHandler.getGetters(klass);
+ Set rs = EntityInfoHandler.getRelatedEntities(klass);
+ Map getters = EntityInfoHandler.getGetters(klass);
List beans = new ArrayList<>();
for (Relationship r : rs) {
if (r.isCollection()) {
@@ -914,7 +911,7 @@ private List getDependentBeans(EntityBaseBean bean) throws IcatE
}
public EntityInfo getEntityInfo(String beanName) throws IcatException {
- return eiHandler.getEntityInfo(beanName);
+ return EntityInfoHandler.getEntityInfo(beanName);
}
@SuppressWarnings("unchecked")
@@ -1188,8 +1185,8 @@ private void isUnique(EntityBaseBean bean, EntityManager manager) throws IcatExc
if (other != null) {
Class extends EntityBaseBean> entityClass = bean.getClass();
- Map getters = eiHandler.getGetters(entityClass);
- List constraint = eiHandler.getConstraintFields(entityClass);
+ Map getters = EntityInfoHandler.getGetters(entityClass);
+ List constraint = EntityInfoHandler.getConstraintFields(entityClass);
StringBuilder erm = new StringBuilder();
for (Field f : constraint) {
@@ -1214,8 +1211,8 @@ private void isUnique(EntityBaseBean bean, EntityManager manager) throws IcatExc
private void isValid(EntityBaseBean bean) throws IcatException {
logger.trace("Checking validity of {}", bean);
Class extends EntityBaseBean> klass = bean.getClass();
- List notNullFields = eiHandler.getNotNullableFields(klass);
- Map getters = eiHandler.getGetters(klass);
+ List notNullFields = EntityInfoHandler.getNotNullableFields(klass);
+ Map getters = EntityInfoHandler.getGetters(klass);
for (Field field : notNullFields) {
@@ -1233,7 +1230,7 @@ private void isValid(EntityBaseBean bean) throws IcatException {
}
}
- Map stringFields = eiHandler.getStringFields(klass);
+ Map stringFields = EntityInfoHandler.getStringFields(klass);
for (Entry entry : stringFields.entrySet()) {
Field field = entry.getKey();
Integer length = entry.getValue();
@@ -1342,8 +1339,8 @@ public void logout(String sessionId, EntityManager manager, UserTransaction user
public EntityBaseBean lookup(EntityBaseBean bean, EntityManager manager) throws IcatException {
Class extends EntityBaseBean> entityClass = bean.getClass();
- Map getters = eiHandler.getGetters(entityClass);
- List constraint = eiHandler.getConstraintFields(entityClass);
+ Map getters = EntityInfoHandler.getGetters(entityClass);
+ List constraint = EntityInfoHandler.getConstraintFields(entityClass);
if (constraint.isEmpty()) {
return null;
}
@@ -1528,11 +1525,9 @@ public List luceneInvestigations(String userName, String u
public void lucenePopulate(String entityName, long minid, EntityManager manager) throws IcatException {
if (luceneActive) {
- try {
- Class.forName(Constants.ENTITY_PREFIX + entityName);
- } catch (ClassNotFoundException e) {
- throw new IcatException(IcatExceptionType.BAD_PARAMETER, e.getMessage());
- }
+ // Throws IcatException if entityName is not an ICAT entity
+ EntityInfoHandler.getClass(entityName);
+
lucene.populate(entityName, minid);
}
}
@@ -1541,8 +1536,8 @@ public void lucenePopulate(String entityName, long minid, EntityManager manager)
// would be processed by JPA which gets confused by it.
private void merge(EntityBaseBean thisBean, Object fromBean, EntityManager manager) throws IcatException {
Class extends EntityBaseBean> klass = thisBean.getClass();
- Map setters = eiHandler.getSettersForUpdate(klass);
- Map getters = eiHandler.getGetters(klass);
+ Map setters = EntityInfoHandler.getSettersForUpdate(klass);
+ Map getters = EntityInfoHandler.getGetters(klass);
for (Entry fieldAndMethod : setters.entrySet()) {
Field field = fieldAndMethod.getKey();
@@ -1572,12 +1567,12 @@ private void merge(EntityBaseBean thisBean, Object fromBean, EntityManager manag
private void parseEntity(EntityBaseBean bean, JsonObject contents, Class extends EntityBaseBean> klass,
EntityManager manager, List creates, Map localUpdates,
boolean create, String userId) throws IcatException {
- Map fieldsByName = eiHandler.getFieldsByName(klass);
- Set updaters = eiHandler.getSettersForUpdate(klass).keySet();
- Map setters = eiHandler.getSetters(klass);
- Map rels = eiHandler.getRelationshipsByName(klass);
- Map getters = eiHandler.getGettersFromName(klass);
- Set relInKey = eiHandler.getRelInKey(klass);
+ Map fieldsByName = EntityInfoHandler.getFieldsByName(klass);
+ Set updaters = EntityInfoHandler.getSettersForUpdate(klass).keySet();
+ Map setters = EntityInfoHandler.getSetters(klass);
+ Map rels = EntityInfoHandler.getRelationshipsByName(klass);
+ Map getters = EntityInfoHandler.getGettersFromName(klass);
+ Set relInKey = EntityInfoHandler.getRelInKey(klass);
boolean deleteAllowed = false;
if (!create) {
@@ -2057,7 +2052,7 @@ private EntityBaseBean writeOne(JsonObject entity, EntityManager manager, String
Entry entry = entity.entrySet().iterator().next();
String beanName = entry.getKey();
- Class klass = EntityInfoHandler.getClass(beanName);
+ Class extends EntityBaseBean> klass = EntityInfoHandler.getClass(beanName);
JsonValue value = entry.getValue();
if (value.getValueType() != ValueType.OBJECT) {
throw new IcatException(IcatExceptionType.BAD_PARAMETER,
@@ -2144,8 +2139,8 @@ private EntityBaseBean writeOne(JsonObject entity, EntityManager manager, String
for (Entry, List> pair : beansByClass.entrySet()) {
Class extends EntityBaseBean> entityClass = pair.getKey();
List beans = pair.getValue();
- Map getters = eiHandler.getGetters(entityClass);
- List constraint = eiHandler.getConstraintFields(entityClass);
+ Map getters = EntityInfoHandler.getGetters(entityClass);
+ List constraint = EntityInfoHandler.getConstraintFields(entityClass);
if (!constraint.isEmpty()) {
for (EntityBaseBean bean1 : beans) {
for (EntityBaseBean bean2 : beans) {
@@ -2212,7 +2207,7 @@ public long cloneEntity(String userId, String beanName, long id, String keys, En
long startMillis = log ? System.currentTimeMillis() : 0;
logger.info("{} cloning {}/{}", userId, beanName, id);
- Class klass = EntityInfoHandler.getClass(beanName);
+ Class extends EntityBaseBean> klass = EntityInfoHandler.getClass(beanName);
EntityBaseBean bean = manager.find(klass, id);
if (bean == null) {
throw new IcatException(IcatExceptionType.NO_SUCH_OBJECT_FOUND, beanName + ":" + id);
@@ -2225,10 +2220,10 @@ public long cloneEntity(String userId, String beanName, long id, String keys, En
}
Map clonedTo = new HashMap<>();
clonedTo.put(bean, clone);
- Map setters = eiHandler.getSettersForUpdate(klass);
- Map getters = eiHandler.getGetters(klass);
- List constraintFields = eiHandler.getConstraintFields(klass);
- Set rs = eiHandler.getRelatedEntities(klass);
+ Map setters = EntityInfoHandler.getSettersForUpdate(klass);
+ Map getters = EntityInfoHandler.getGetters(klass);
+ List constraintFields = EntityInfoHandler.getConstraintFields(klass);
+ Set rs = EntityInfoHandler.getRelatedEntities(klass);
for (Entry fieldAndMethod : setters.entrySet()) {
Field field = fieldAndMethod.getKey();
@@ -2403,9 +2398,9 @@ private void cloneOneToManys(EntityBaseBean bean, EntityBaseBean clone, Class
clonedCollection.add(subClone);
clonedTo.put(c, subClone);
- Map subSetters = eiHandler.getSettersForUpdate(subKlass);
- Map subGetters = eiHandler.getGetters(subKlass);
- Set subRs = eiHandler.getRelatedEntities(subKlass);
+ Map subSetters = EntityInfoHandler.getSettersForUpdate(subKlass);
+ Map subGetters = EntityInfoHandler.getGetters(subKlass);
+ Set subRs = EntityInfoHandler.getRelatedEntities(subKlass);
for (Entry fieldAndMethod : subSetters.entrySet()) {
Field field = fieldAndMethod.getKey();
diff --git a/src/main/java/org/icatproject/core/manager/EntityInfoHandler.java b/src/main/java/org/icatproject/core/manager/EntityInfoHandler.java
index 0a44ed6dc..b8cd4d19c 100644
--- a/src/main/java/org/icatproject/core/manager/EntityInfoHandler.java
+++ b/src/main/java/org/icatproject/core/manager/EntityInfoHandler.java
@@ -9,7 +9,6 @@
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
@@ -18,6 +17,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
+import java.util.stream.Collectors;
import jakarta.json.stream.JsonGenerator;
import jakarta.persistence.CascadeType;
@@ -31,7 +31,6 @@
import jakarta.persistence.UniqueConstraint;
import jakarta.xml.bind.annotation.XmlTransient;
-import org.icatproject.core.Constants;
import org.icatproject.core.IcatException;
import org.icatproject.core.entity.Affiliation;
import org.icatproject.core.entity.Application;
@@ -90,34 +89,32 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-// Note that this does not use a Singleton Bean as there is no need for the
-// extra complexity and
-// that the instance is created statically as we know it will be needed.
+// This class only has static methods and cannot be instantiated
public class EntityInfoHandler {
- private class PrivateEntityInfo {
-
- private Set attributes;
- private String classComment;
- private List constraintFields;
- private Constructor extends EntityBaseBean> constructor;
- public String exportHeader;
- public String exportHeaderAll;
- public String exportNull;
- private Map fieldComments;
- private Map fieldsByName;
- private final Map getters;
- private final List notNullableFields;
- private Set ones;
- private final Set relatedEntities;
- private Map setters;
- private final Map stringFields;
- private final Map updaters;
- private List fields;
- public Map gettersFromName;
- public Map relationshipsByName;
- public Set relInKey;
- private boolean hasLuceneDoc;
+ private static class PrivateEntityInfo {
+
+ final Set attributes;
+ final String classComment;
+ final List constraintFields;
+ final Constructor extends EntityBaseBean> constructor;
+ final String exportHeader;
+ final String exportHeaderAll;
+ final String exportNull;
+ final Map fieldComments;
+ final Map fieldsByName;
+ final Map getters;
+ final List notNullableFields;
+ final Set ones;
+ final Set relatedEntities;
+ final Map setters;
+ final Map stringFields;
+ final Map updaters;
+ final List fields;
+ final Map gettersFromName;
+ final Map relationshipsByName;
+ final Set relInKey;
+ final boolean hasLuceneDoc;
public PrivateEntityInfo(Set rels, List notNullableFields, Map getters,
Map gettersFromName, Map stringFields, Map setters,
@@ -126,31 +123,33 @@ public PrivateEntityInfo(Set rels, List notNullableFields,
Constructor extends EntityBaseBean> constructor, Map fieldByName, String exportHeader,
String exportNull, List fields, String exportHeaderAll,
Map relationshipsByName, Set relInKey, boolean hasLuceneDoc) {
- this.relatedEntities = rels;
- this.notNullableFields = notNullableFields;
- this.getters = getters;
- this.gettersFromName = gettersFromName;
- this.stringFields = stringFields;
- this.setters = setters;
- this.updaters = updaters;
- this.constraintFields = constraintFields;
+
+ // Use copyOf to create unmodifiable collections
+ this.relatedEntities = Set.copyOf(rels);
+ this.notNullableFields = List.copyOf(notNullableFields);
+ this.getters = Map.copyOf(getters);
+ this.gettersFromName = Map.copyOf(gettersFromName);
+ this.stringFields = Map.copyOf(stringFields);
+ this.setters = Map.copyOf(setters);
+ this.updaters = Map.copyOf(updaters);
+ this.constraintFields = List.copyOf(constraintFields);
this.classComment = classComment;
- this.fieldComments = fieldComments;
- this.ones = ones;
- this.attributes = attributes;
+ this.fieldComments = Map.copyOf(fieldComments);
+ this.ones = Set.copyOf(ones);
+ this.attributes = Set.copyOf(attributes);
this.constructor = constructor;
- this.fieldsByName = fieldByName;
+ this.fieldsByName = Map.copyOf(fieldByName);
this.exportHeader = exportHeader;
this.exportNull = exportNull;
- this.fields = fields;
+ this.fields = List.copyOf(fields);
this.exportHeaderAll = exportHeaderAll;
- this.relationshipsByName = relationshipsByName;
- this.relInKey = relInKey;
+ this.relationshipsByName = Map.copyOf(relationshipsByName);
+ this.relInKey = Set.copyOf(relInKey);
this.hasLuceneDoc = hasLuceneDoc;
}
}
- public class Relationship {
+ public static class Relationship {
private final boolean collection;
@@ -158,9 +157,9 @@ public class Relationship {
private final Field field;
- private Method inverseSetter;
+ private final Method inverseSetter;
- private Class extends EntityBaseBean> originBean;
+ private final Class extends EntityBaseBean> originBean;
public Relationship(Class extends EntityBaseBean> originBean, Field field,
Class extends EntityBaseBean> destinationBean, boolean collection, boolean cascaded,
@@ -205,8 +204,12 @@ public String toString() {
}
- private static List alphabeticEntityNames;
- private static List> entities = Arrays.asList(User.class, Grouping.class,
+ private static final Logger logger = LoggerFactory.getLogger(EntityInfoHandler.class);
+
+ private static final String[] SYSTEM_ATTRIBUTES = { "createId", "createTime", "modId", "modTime" };
+
+ // All entities in export order
+ private static final List> ENTITIES = List.of(User.class, Grouping.class,
UserGroup.class, Rule.class, PublicStep.class, Facility.class, DatafileFormat.class, Application.class,
Instrument.class, InvestigationType.class, DatasetType.class, ParameterType.class, SampleType.class,
Investigation.class, Sample.class, Dataset.class, Datafile.class, FacilityCycle.class, DataCollection.class,
@@ -219,71 +222,45 @@ public String toString() {
InvestigationFunding.class, InvestigationUser.class, InvestigationGroup.class, StudyInvestigation.class,
InvestigationInstrument.class, InstrumentScientist.class, DatasetInstrument.class,
InvestigationFacilityCycle.class);
- private static Set entityNames = new HashSet<>();
- private static String[] systemAttributes = { "createId", "createTime", "modId", "modTime" };
+ // All entity names in export order
+ private static final List EXPORT_ENTITY_NAMES =
+ ENTITIES.stream().map((entity) -> entity.getSimpleName()).collect(Collectors.toUnmodifiableList());
- private static List exportEntityNames = new ArrayList<>();
+ // All entity names in alphabetical order
+ private static final List ENTITY_NAMES =
+ ENTITIES.stream().map((entity) -> entity.getSimpleName()).sorted().collect(Collectors.toUnmodifiableList());
- public static EntityInfoHandler instance = new EntityInfoHandler();
+ // Map of entity name -> entity class
+ private static final Map> ENTITY_NAME_MAP =
+ ENTITIES.stream().collect(Collectors.toUnmodifiableMap((entity) -> entity.getSimpleName(), (entity) -> entity));
- protected final static Logger logger = LoggerFactory.getLogger(EntityInfoHandler.class);
+ // Map of entity class -> PrivateEntityInfo
+ private static final Map, PrivateEntityInfo> PRIVATE_ENTITY_INFO_MAP =
+ ENTITIES.stream().collect(Collectors.toUnmodifiableMap((entity) -> entity, (entity) -> buildEi(entity)));
- static {
- for (Class extends EntityBaseBean> entity : entities) {
- entityNames.add(entity.getSimpleName());
- exportEntityNames.add(entity.getSimpleName());
- }
- alphabeticEntityNames = new ArrayList<>(entityNames);
- Collections.sort(alphabeticEntityNames);
- }
+ public static Class extends EntityBaseBean> getClass(String tableName) throws IcatException {
+ Class extends EntityBaseBean> entityClass = ENTITY_NAME_MAP.get(tableName);
- private final static Comparator super Field> fieldComparator = new Comparator() {
- @Override
- public int compare(Field o1, Field o2) {
- return o1.getName().compareTo(o2.getName());
+ if (entityClass == null) {
+ throw new IcatException(IcatException.IcatExceptionType.BAD_PARAMETER, tableName + " is not an ICAT entity");
}
- };
-
- public static Set getAlphabeticEntityNames() {
- return entityNames;
- }
- public static Class getClass(String tableName) throws IcatException {
- try {
- final Class> klass = Class.forName(Constants.ENTITY_PREFIX + tableName);
- if (EntityBaseBean.class.isAssignableFrom(klass)) {
- @SuppressWarnings("unchecked")
- final Class eklass = (Class) klass;
- return eklass;
- } else {
- throw new IcatException(IcatException.IcatExceptionType.BAD_PARAMETER,
- tableName + " is not an EntityBaseBean");
- }
- } catch (final ClassNotFoundException e) {
- throw new IcatException(IcatException.IcatExceptionType.BAD_PARAMETER,
- tableName + " is not an EntityBaseBean");
- }
+ return entityClass;
}
public static List getEntityNamesList() {
- return alphabeticEntityNames;
+ return ENTITY_NAMES;
};
public static List getExportEntityNames() {
- return exportEntityNames;
+ return EXPORT_ENTITY_NAMES;
};
- public static synchronized EntityInfoHandler getInstance() {
- return instance;
- }
-
- private final HashMap, PrivateEntityInfo> map = new HashMap, PrivateEntityInfo>();
-
private EntityInfoHandler() {
}
- private PrivateEntityInfo buildEi(Class extends EntityBaseBean> objectClass) throws IcatException {
+ private static PrivateEntityInfo buildEi(Class extends EntityBaseBean> objectClass) {
logger.debug("Building PrivateEntityInfo for " + objectClass);
List fields = new ArrayList();
Class> cobj = objectClass;
@@ -292,7 +269,7 @@ private PrivateEntityInfo buildEi(Class extends EntityBaseBean> objectClass) t
cobj = cobj.getSuperclass();
}
- Collections.sort(fields, fieldComparator);
+ fields.sort(Comparator.comparing(Field::getName));
Map fieldsByName = new HashMap<>();
Set attributes = new HashSet();
@@ -309,19 +286,14 @@ private PrivateEntityInfo buildEi(Class extends EntityBaseBean> objectClass) t
if (oneToMany != null) {
all = Arrays.asList(oneToMany.cascade()).contains(CascadeType.ALL);
if (!all && oneToMany.cascade().length != 0) {
- throw new IcatException(IcatException.IcatExceptionType.INTERNAL,
- "Cascade must be all or nothing " + objectClass.getSimpleName() + "."
- + field.getName());
+ throw new IllegalStateException("Cascade must be all or nothing " + objectClass.getSimpleName() + "." + field.getName());
}
mappedBy = oneToMany.mappedBy();
if (mappedBy == null) {
- throw new IcatException(IcatException.IcatExceptionType.INTERNAL,
- "MappedBy must be set for " + objectClass.getSimpleName() + "." + field.getName());
+ throw new IllegalStateException("MappedBy must be set for " + objectClass.getSimpleName() + "." + field.getName());
}
} else {
- throw new IcatException(IcatException.IcatExceptionType.INTERNAL,
- "Looks like a one to many relationship but not marked as such "
- + objectClass.getSimpleName() + "." + field.getName());
+ throw new IllegalStateException("Looks like a one to many relationship but not marked as such " + objectClass.getSimpleName() + "." + field.getName());
}
final ParameterizedType pt = (ParameterizedType) field.getGenericType();
final Type[] args = pt.getActualTypeArguments();
@@ -332,24 +304,19 @@ private PrivateEntityInfo buildEi(Class extends EntityBaseBean> objectClass) t
if (EntityBaseBean.class.isAssignableFrom(argc)) {
@SuppressWarnings("unchecked")
final Class extends EntityBaseBean> argc2 = (Class extends EntityBaseBean>) argc;
- try {
- String name = "set" + Character.toUpperCase(mappedBy.charAt(0)) + mappedBy.substring(1);
- Method[] ms = argc2.getMethods();
- boolean inv = false;
- for (Method m : ms) {
- if (m.getName().equals(name)) {
- rels.add(new Relationship(objectClass, field, argc2, true, all, m));
- inv = true;
- break;
- }
- }
- if (!inv) {
- throw new IcatException(IcatException.IcatExceptionType.INTERNAL,
- "Inverse relationship not found " + objectClass.getSimpleName() + "."
- + field.getName());
+
+ String name = "set" + Character.toUpperCase(mappedBy.charAt(0)) + mappedBy.substring(1);
+ Method[] ms = argc2.getMethods();
+ boolean inv = false;
+ for (Method m : ms) {
+ if (m.getName().equals(name)) {
+ rels.add(new Relationship(objectClass, field, argc2, true, all, m));
+ inv = true;
+ break;
}
- } catch (SecurityException e) {
- throw new IcatException(IcatException.IcatExceptionType.INTERNAL, e.getMessage());
+ }
+ if (!inv) {
+ throw new IllegalStateException("Inverse relationship not found " + objectClass.getSimpleName() + "." + field.getName());
}
}
}
@@ -360,14 +327,10 @@ private PrivateEntityInfo buildEi(Class extends EntityBaseBean> objectClass) t
if (manyToOne != null) {
all = Arrays.asList(manyToOne.cascade()).contains(CascadeType.ALL);
if (!all && manyToOne.cascade().length != 0) {
- throw new IcatException(IcatException.IcatExceptionType.INTERNAL,
- "Cascade must be all or nothing " + objectClass.getSimpleName() + "."
- + field.getName());
+ throw new IllegalStateException("Cascade must be all or nothing " + objectClass.getSimpleName() + "." + field.getName());
}
} else {
- throw new IcatException(IcatException.IcatExceptionType.INTERNAL,
- "Looks like a many to one relationship but not marked as such "
- + objectClass.getSimpleName() + "." + field.getName());
+ throw new IllegalStateException("Looks like a many to one relationship but not marked as such " + objectClass.getSimpleName() + "." + field.getName());
}
@SuppressWarnings("unchecked")
final Class extends EntityBaseBean> argc2 = (Class extends EntityBaseBean>) field.getType();
@@ -408,11 +371,11 @@ private PrivateEntityInfo buildEi(Class extends EntityBaseBean> objectClass) t
Method method;
try {
method = objc.getMethod("get" + prop);
- } catch (final NoSuchMethodException e) {
+ } catch (NoSuchMethodException e1) {
try {
method = objc.getMethod("is" + prop);
- } catch (final Exception e1) {
- throw new IcatException(IcatException.IcatExceptionType.INTERNAL, "" + e);
+ } catch (NoSuchMethodException e2) {
+ throw new IllegalStateException("No method get" + prop + "() or is" + prop+ "() found in class " + objc.getName());
}
}
getters.put(field, method);
@@ -477,15 +440,13 @@ private PrivateEntityInfo buildEi(Class extends EntityBaseBean> objectClass) t
setters.put(field, m);
if (settable) {
if (updaters.put(field, m) != null) {
- throw new IcatException(IcatException.IcatExceptionType.INTERNAL,
- "set" + prop + " is ambiguous");
+ throw new IllegalStateException("set" + prop + " is ambiguous");
}
}
}
}
if (settable && updaters.get(field) == null) {
- throw new IcatException(IcatException.IcatExceptionType.INTERNAL,
- "set" + prop + " not found for " + objc.getSimpleName());
+ throw new IllegalStateException("set" + prop + " not found for " + objc.getSimpleName());
}
if (getters.get(field).getReturnType().equals(String.class)) {
@@ -504,20 +465,17 @@ private PrivateEntityInfo buildEi(Class extends EntityBaseBean> objectClass) t
for (String colNam : Arrays.asList(constraint.columnNames())) {
Field col = dbCols.get(colNam);
if (col == null) {
- throw new IcatException(IcatException.IcatExceptionType.INTERNAL,
- "Column " + colNam + " mentioned in UniqueConstraint of " + objectClass.getSimpleName()
+ throw new IllegalStateException("Column " + colNam + " mentioned in UniqueConstraint of " + objectClass.getSimpleName()
+ " table is not present in entity");
}
if (!nnf.contains(col)) {
- throw new IcatException(IcatException.IcatExceptionType.INTERNAL,
- "Column " + colNam + " mentioned in UniqueConstraint of " + objectClass.getSimpleName()
+ throw new IllegalStateException("Column " + colNam + " mentioned in UniqueConstraint of " + objectClass.getSimpleName()
+ " table must be annotated as 'nullable = false'");
}
constraintFields.add(col);
}
} else if (ncons > 1) {
- throw new IcatException(IcatException.IcatExceptionType.INTERNAL, "Problem with "
- + objectClass.getSimpleName() + ": at most one constraint may be defined for an ICAT entity");
+ throw new IllegalStateException("Problem with " + objectClass.getSimpleName() + ": at most one constraint may be defined for an ICAT entity");
}
}
@@ -527,8 +485,8 @@ private PrivateEntityInfo buildEi(Class extends EntityBaseBean> objectClass) t
Constructor extends EntityBaseBean> constructor = null;
try {
constructor = objectClass.getConstructor();
- } catch (Exception e) {
- throw new IcatException(IcatException.IcatExceptionType.INTERNAL, e.getClass() + " " + e.getMessage());
+ } catch (NoSuchMethodException e) {
+ throw new IllegalStateException("Zero-argument constructor not found for class " + objectClass.getName(), e);
}
StringBuilder exportNull = new StringBuilder();
@@ -548,7 +506,7 @@ private PrivateEntityInfo buildEi(Class extends EntityBaseBean> objectClass) t
nAll = 1;
}
- for (String s : systemAttributes) {
+ for (String s : SYSTEM_ATTRIBUTES) {
exportHeaderAll.append(sepAll);
sepAll = ",";
exportHeaderAll.append(s + ":" + nAll++);
@@ -622,67 +580,40 @@ private PrivateEntityInfo buildEi(Class extends EntityBaseBean> objectClass) t
relInKey, hasLuceneDoc);
}
+ private static PrivateEntityInfo getPrivateEntityInfo(Class extends EntityBaseBean> objectClass) {
+ PrivateEntityInfo ei = PRIVATE_ENTITY_INFO_MAP.get(objectClass);
+
+ if (ei == null) {
+ // Should never happen because the map contains all non-abstract classes that extend EntityBaseBean
+ throw new IllegalStateException("Class not found in PRIVATE_ENTITY_INFO_MAP: " + objectClass.getName());
+ }
+
+ return ei;
+ };
+
/**
* Set of fields that are "simple attributes". This excludes relationships,
* id, createId, CreateTime, modId and modTime
*/
- public Set getAttributes(Class extends EntityBaseBean> objectClass) throws IcatException {
- PrivateEntityInfo ei = null;
- synchronized (this.map) {
- ei = this.map.get(objectClass);
- if (ei == null) {
- ei = this.buildEi(objectClass);
- this.map.put(objectClass, ei);
- }
- return ei.attributes;
- }
+ public static Set getAttributes(Class extends EntityBaseBean> objectClass) {
+ return getPrivateEntityInfo(objectClass).attributes;
}
- public String getClassComment(Class extends EntityBaseBean> objectClass) throws IcatException {
- PrivateEntityInfo ei = null;
- synchronized (this.map) {
- ei = this.map.get(objectClass);
- if (ei == null) {
- ei = this.buildEi(objectClass);
- this.map.put(objectClass, ei);
- }
- return ei.classComment;
- }
+ public static String getClassComment(Class extends EntityBaseBean> objectClass) {
+ return getPrivateEntityInfo(objectClass).classComment;
}
- public List getConstraintFields(Class extends EntityBaseBean> objectClass) throws IcatException {
- PrivateEntityInfo ei = null;
- synchronized (this.map) {
- ei = this.map.get(objectClass);
- if (ei == null) {
- ei = this.buildEi(objectClass);
- this.map.put(objectClass, ei);
- }
- return ei.constraintFields;
- }
+ public static List getConstraintFields(Class extends EntityBaseBean> objectClass) {
+ return getPrivateEntityInfo(objectClass).constraintFields;
}
- public Constructor extends EntityBaseBean> getConstructor(Class extends EntityBaseBean> objectClass)
- throws IcatException {
- PrivateEntityInfo ei = null;
- synchronized (this.map) {
- ei = this.map.get(objectClass);
- if (ei == null) {
- ei = this.buildEi(objectClass);
- this.map.put(objectClass, ei);
- }
- return ei.constructor;
- }
+ public static Constructor extends EntityBaseBean> getConstructor(Class extends EntityBaseBean> objectClass) {
+ return getPrivateEntityInfo(objectClass).constructor;
}
- @SuppressWarnings("unchecked")
- public EntityInfo getEntityInfo(String beanName) throws IcatException {
- Class extends EntityBaseBean> beanClass;
- try {
- beanClass = (Class extends EntityBaseBean>) Class.forName(Constants.ENTITY_PREFIX + beanName);
- } catch (ClassNotFoundException e) {
- throw new IcatException(IcatException.IcatExceptionType.BAD_PARAMETER, beanName + " is not an ICAT entity");
- }
+ public static EntityInfo getEntityInfo(String beanName) throws IcatException {
+ Class extends EntityBaseBean> beanClass = getClass(beanName);
+
EntityInfo entityInfo = new EntityInfo();
entityInfo.setClassComment(getClassComment(beanClass));
List constraint = getConstraintFields(beanClass);
@@ -724,113 +655,49 @@ public EntityInfo getEntityInfo(String beanName) throws IcatException {
}
- public String getExportHeader(Class extends EntityBaseBean> objectClass) throws IcatException {
- PrivateEntityInfo ei = null;
- synchronized (this.map) {
- ei = this.map.get(objectClass);
- if (ei == null) {
- ei = this.buildEi(objectClass);
- this.map.put(objectClass, ei);
- }
- return ei.exportHeader;
- }
+ public static String getExportHeader(Class extends EntityBaseBean> objectClass) {
+ return getPrivateEntityInfo(objectClass).exportHeader;
}
- public String getExportHeaderAll(Class extends EntityBaseBean> objectClass) throws IcatException {
- PrivateEntityInfo ei = null;
- synchronized (this.map) {
- ei = this.map.get(objectClass);
- if (ei == null) {
- ei = this.buildEi(objectClass);
- this.map.put(objectClass, ei);
- }
- return ei.exportHeaderAll;
- }
+ public static String getExportHeaderAll(Class extends EntityBaseBean> objectClass) {
+ return getPrivateEntityInfo(objectClass).exportHeaderAll;
}
- public String getExportNull(Class extends EntityBaseBean> objectClass) throws IcatException {
- PrivateEntityInfo ei = null;
- synchronized (this.map) {
- ei = this.map.get(objectClass);
- if (ei == null) {
- ei = this.buildEi(objectClass);
- this.map.put(objectClass, ei);
- }
- return ei.exportNull;
- }
+ public static String getExportNull(Class extends EntityBaseBean> objectClass) {
+ return getPrivateEntityInfo(objectClass).exportNull;
}
- public Map getFieldComments(Class extends EntityBaseBean> objectClass) throws IcatException {
- PrivateEntityInfo ei = null;
- synchronized (this.map) {
- ei = this.map.get(objectClass);
- if (ei == null) {
- ei = this.buildEi(objectClass);
- this.map.put(objectClass, ei);
- }
- return ei.fieldComments;
- }
+ public static Map getFieldComments(Class extends EntityBaseBean> objectClass) {
+ return getPrivateEntityInfo(objectClass).fieldComments;
}
/**
* Returns all user settable fields (not id, createId, modId, createTime nor
* modTime
*/
- public List getFields(Class extends EntityBaseBean> objectClass) throws IcatException {
- PrivateEntityInfo ei = null;
- synchronized (this.map) {
- ei = this.map.get(objectClass);
- if (ei == null) {
- ei = this.buildEi(objectClass);
- this.map.put(objectClass, ei);
- }
- return ei.fields;
- }
+ public static List getFields(Class extends EntityBaseBean> objectClass) {
+ return getPrivateEntityInfo(objectClass).fields;
}
- public Map getFieldsByName(Class extends EntityBaseBean> objectClass) throws IcatException {
- PrivateEntityInfo ei = null;
- synchronized (this.map) {
- ei = this.map.get(objectClass);
- if (ei == null) {
- ei = this.buildEi(objectClass);
- this.map.put(objectClass, ei);
- }
- return ei.fieldsByName;
- }
+ public static Map getFieldsByName(Class extends EntityBaseBean> objectClass) {
+ return getPrivateEntityInfo(objectClass).fieldsByName;
}
/**
* Map from field to getter for all fields
*/
- public Map getGetters(Class extends EntityBaseBean> objectClass) throws IcatException {
- PrivateEntityInfo ei = null;
- synchronized (this.map) {
- ei = this.map.get(objectClass);
- if (ei == null) {
- ei = this.buildEi(objectClass);
- this.map.put(objectClass, ei);
- }
- return ei.getters;
- }
+ public static Map getGetters(Class extends EntityBaseBean> objectClass) {
+ return getPrivateEntityInfo(objectClass).getters;
}
/**
* Map from field name to getter for all fields
*/
- public Map getGettersFromName(Class extends EntityBaseBean> objectClass) throws IcatException {
- PrivateEntityInfo ei = null;
- synchronized (this.map) {
- ei = this.map.get(objectClass);
- if (ei == null) {
- ei = this.buildEi(objectClass);
- this.map.put(objectClass, ei);
- }
- return ei.gettersFromName;
- }
+ public static Map getGettersFromName(Class extends EntityBaseBean> objectClass) {
+ return getPrivateEntityInfo(objectClass).gettersFromName;
}
- private List getNormalFields(Class> cobj) {
+ private static List getNormalFields(Class> cobj) {
List fields = new ArrayList(Arrays.asList(cobj.getDeclaredFields()));
final Iterator iter = fields.iterator();
while (iter.hasNext()) {
@@ -849,40 +716,16 @@ private List getNormalFields(Class> cobj) {
return fields;
}
- public List getNotNullableFields(Class extends EntityBaseBean> objectClass) throws IcatException {
- PrivateEntityInfo ei = null;
- synchronized (this.map) {
- ei = this.map.get(objectClass);
- if (ei == null) {
- ei = this.buildEi(objectClass);
- this.map.put(objectClass, ei);
- }
- return ei.notNullableFields;
- }
+ public static List getNotNullableFields(Class extends EntityBaseBean> objectClass) {
+ return getPrivateEntityInfo(objectClass).notNullableFields;
}
- public Set getOnes(Class extends EntityBaseBean> objectClass) throws IcatException {
- PrivateEntityInfo ei = null;
- synchronized (this.map) {
- ei = this.map.get(objectClass);
- if (ei == null) {
- ei = this.buildEi(objectClass);
- this.map.put(objectClass, ei);
- }
- return ei.ones;
- }
+ public static Set getOnes(Class extends EntityBaseBean> objectClass) {
+ return getPrivateEntityInfo(objectClass).ones;
}
- public Set getRelatedEntities(Class extends EntityBaseBean> objectClass) throws IcatException {
- PrivateEntityInfo ei = null;
- synchronized (this.map) {
- ei = this.map.get(objectClass);
- if (ei == null) {
- ei = this.buildEi(objectClass);
- this.map.put(objectClass, ei);
- }
- return ei.relatedEntities;
- }
+ public static Set getRelatedEntities(Class extends EntityBaseBean> objectClass) {
+ return getPrivateEntityInfo(objectClass).relatedEntities;
}
/**
@@ -890,92 +733,43 @@ public Set getRelatedEntities(Class extends EntityBaseBean> obje
*
* @throws IcatException
*/
- public Map getRelationshipsByName(Class extends EntityBaseBean> objectClass)
- throws IcatException {
- PrivateEntityInfo ei = null;
- synchronized (this.map) {
- ei = this.map.get(objectClass);
- if (ei == null) {
- ei = this.buildEi(objectClass);
- this.map.put(objectClass, ei);
- }
- return ei.relationshipsByName;
- }
+ public static Map getRelationshipsByName(Class extends EntityBaseBean> objectClass) {
+ return getPrivateEntityInfo(objectClass).relationshipsByName;
}
- public Set getRelInKey(Class extends EntityBaseBean> objectClass) throws IcatException {
- PrivateEntityInfo ei = null;
- synchronized (this.map) {
- ei = this.map.get(objectClass);
- if (ei == null) {
- ei = this.buildEi(objectClass);
- this.map.put(objectClass, ei);
- }
- return ei.relInKey;
- }
+ public static Set getRelInKey(Class extends EntityBaseBean> objectClass) {
+ return getPrivateEntityInfo(objectClass).relInKey;
}
/**
* Map from field to setter for all fields including id but not for
* createId, modId, createTime nor modTime
*/
- public Map getSetters(Class extends EntityBaseBean> objectClass) throws IcatException {
- PrivateEntityInfo ei = null;
- synchronized (this.map) {
- ei = this.map.get(objectClass);
- if (ei == null) {
- ei = this.buildEi(objectClass);
- this.map.put(objectClass, ei);
- }
- return ei.setters;
- }
+ public static Map getSetters(Class extends EntityBaseBean> objectClass) {
+ return getPrivateEntityInfo(objectClass).setters;
}
/**
* Returns the setters for those fields that are not one to many
* relationships and not id, createId, modId, createTime nor modTime
*/
- public Map getSettersForUpdate(Class extends EntityBaseBean> objectClass) throws IcatException {
- PrivateEntityInfo ei = null;
- synchronized (this.map) {
- ei = this.map.get(objectClass);
- if (ei == null) {
- ei = this.buildEi(objectClass);
- this.map.put(objectClass, ei);
- }
- return ei.updaters;
- }
+ public static Map getSettersForUpdate(Class extends EntityBaseBean> objectClass) {
+ return getPrivateEntityInfo(objectClass).updaters;
}
/**
* Returns all string fields except for createId and modId
*/
- public Map getStringFields(Class extends EntityBaseBean> objectClass) throws IcatException {
- PrivateEntityInfo ei = null;
- synchronized (this.map) {
- ei = this.map.get(objectClass);
- if (ei == null) {
- ei = this.buildEi(objectClass);
- this.map.put(objectClass, ei);
- }
- return ei.stringFields;
- }
+ public static Map getStringFields(Class extends EntityBaseBean> objectClass) {
+ return getPrivateEntityInfo(objectClass).stringFields;
}
/** Return true if getDoc() method exists else false */
- public boolean hasLuceneDoc(Class extends EntityBaseBean> objectClass) throws IcatException {
- PrivateEntityInfo ei = null;
- synchronized (this.map) {
- ei = this.map.get(objectClass);
- if (ei == null) {
- ei = this.buildEi(objectClass);
- this.map.put(objectClass, ei);
- }
- return ei.hasLuceneDoc;
- }
+ public static boolean hasLuceneDoc(Class extends EntityBaseBean> objectClass) {
+ return getPrivateEntityInfo(objectClass).hasLuceneDoc;
}
- private int setRelHeader(int n, Field field, StringBuilder exportHeader, StringBuilder exportNull, boolean con) {
+ private static int setRelHeader(int n, Field field, StringBuilder exportHeader, StringBuilder exportNull, boolean con) {
Set conColumns = new HashSet<>();
Table tableAnnot = field.getType().getAnnotation(Table.class);
@@ -998,7 +792,7 @@ private int setRelHeader(int n, Field field, StringBuilder exportHeader, StringB
fields.addAll(getNormalFields(cobj));
cobj = cobj.getSuperclass();
}
- Collections.sort(fields, fieldComparator);
+ fields.sort(Comparator.comparing(Field::getName));
String sep = "";
for (Field f : fields) {
diff --git a/src/main/java/org/icatproject/core/manager/GateKeeper.java b/src/main/java/org/icatproject/core/manager/GateKeeper.java
index 5629fa532..a1d760d00 100644
--- a/src/main/java/org/icatproject/core/manager/GateKeeper.java
+++ b/src/main/java/org/icatproject/core/manager/GateKeeper.java
@@ -68,8 +68,6 @@ public int compare(String o1, String o2) {
}
};
- private static final EntityInfoHandler eiHandler = EntityInfoHandler.getInstance();
-
private final static Pattern tsRegExp = Pattern
.compile("\\{\\s*ts\\s+\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}:\\d{2}\\s*\\}");
@@ -367,8 +365,8 @@ public void performUpdateAuthorisation(String user, EntityBaseBean bean, JsonObj
logger.info("Consider {}", contents);
Class extends EntityBaseBean> klass = bean.getClass();
String simpleName = klass.getSimpleName();
- Set updaters = eiHandler.getSettersForUpdate(klass).keySet();
- Map fieldsByName = eiHandler.getFieldsByName(klass);
+ Set updaters = EntityInfoHandler.getSettersForUpdate(klass).keySet();
+ Map fieldsByName = EntityInfoHandler.getFieldsByName(klass);
for (Entry fentry : contents.entrySet()) {
String fName = fentry.getKey();
diff --git a/src/main/java/org/icatproject/core/manager/LuceneManager.java b/src/main/java/org/icatproject/core/manager/LuceneManager.java
index adad614fb..f52e39bd4 100644
--- a/src/main/java/org/icatproject/core/manager/LuceneManager.java
+++ b/src/main/java/org/icatproject/core/manager/LuceneManager.java
@@ -46,7 +46,6 @@
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
-import org.icatproject.core.Constants;
import org.icatproject.core.IcatException;
import org.icatproject.core.IcatException.IcatExceptionType;
import org.icatproject.core.entity.EntityBaseBean;
@@ -118,7 +117,7 @@ public IndexSome(String entityName, List ids, EntityManagerFactory entityM
try {
logger.debug("About to index {} {} records", ids.size(), entityName);
this.entityName = entityName;
- klass = (Class extends EntityBaseBean>) Class.forName(Constants.ENTITY_PREFIX + entityName);
+ klass = EntityInfoHandler.getClass(entityName);
this.ids = ids;
manager = entityManagerFactory.createEntityManager();
this.start = start;
@@ -130,7 +129,7 @@ public IndexSome(String entityName, List ids, EntityManagerFactory entityM
@Override
public Long call() throws Exception {
- if (eiHandler.hasLuceneDoc(klass)) {
+ if (EntityInfoHandler.hasLuceneDoc(klass)) {
URI uri = new URIBuilder(luceneApi.server).setPath(LuceneApi.basePath + "/addNow/" + entityName)
.build();
@@ -299,8 +298,6 @@ public void run() {
}
}
- private static EntityInfoHandler eiHandler = EntityInfoHandler.getInstance();
-
final static Logger logger = LoggerFactory.getLogger(LuceneManager.class);
final static Marker fatal = MarkerFactory.getMarker("FATAL");
@@ -347,7 +344,7 @@ public void run() {
public void addDocument(EntityBaseBean bean) throws IcatException {
String entityName = bean.getClass().getSimpleName();
- if (eiHandler.hasLuceneDoc(bean.getClass()) && entitiesToIndex.contains(entityName)) {
+ if (EntityInfoHandler.hasLuceneDoc(bean.getClass()) && entitiesToIndex.contains(entityName)) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (JsonGenerator gen = Json.createGenerator(baos)) {
gen.writeStartArray();
@@ -425,7 +422,7 @@ public LuceneSearchResult datasetsAfter(Long uid, int blockSize) throws IcatExce
}
public void deleteDocument(EntityBaseBean bean) throws IcatException {
- if (eiHandler.hasLuceneDoc(bean.getClass())) {
+ if (EntityInfoHandler.hasLuceneDoc(bean.getClass())) {
String entityName = bean.getClass().getSimpleName();
Long id = bean.getId();
enqueue(entityName, null, id);
@@ -540,7 +537,7 @@ public void populate(String entityName, long minid) throws IcatException {
public void updateDocument(EntityBaseBean bean) throws IcatException {
String entityName = bean.getClass().getSimpleName();
- if (eiHandler.hasLuceneDoc(bean.getClass()) && entitiesToIndex.contains(entityName)) {
+ if (EntityInfoHandler.hasLuceneDoc(bean.getClass()) && entitiesToIndex.contains(entityName)) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (JsonGenerator gen = Json.createGenerator(baos)) {
gen.writeStartArray();
@@ -551,4 +548,4 @@ public void updateDocument(EntityBaseBean bean) throws IcatException {
}
}
-}
\ No newline at end of file
+}
diff --git a/src/main/java/org/icatproject/core/manager/NotificationMessage.java b/src/main/java/org/icatproject/core/manager/NotificationMessage.java
index 6f48f916d..368e492b4 100644
--- a/src/main/java/org/icatproject/core/manager/NotificationMessage.java
+++ b/src/main/java/org/icatproject/core/manager/NotificationMessage.java
@@ -34,12 +34,6 @@ public String getOperation() {
}
}
- private final static EntityInfoHandler entityInfoHandler = EntityInfoHandler.getInstance();
-
- public static EntityInfoHandler getEntityinfohandler() {
- return entityInfoHandler;
- }
-
private Message message;
public NotificationMessage(Operation operation, EntityBaseBean bean, EntityManager manager,
diff --git a/src/main/java/org/icatproject/core/manager/Porter.java b/src/main/java/org/icatproject/core/manager/Porter.java
index 55de82560..bb95ff4cc 100644
--- a/src/main/java/org/icatproject/core/manager/Porter.java
+++ b/src/main/java/org/icatproject/core/manager/Porter.java
@@ -94,8 +94,6 @@ public enum DuplicateAction {
private final static DateFormat dfZoned = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
private final static DateFormat dfNoZone = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
- private final static EntityInfoHandler eiHandler = EntityInfoHandler.getInstance();
-
@PostConstruct
void init() {
importCacheSize = propertyHandler.getImportCacheSize();
@@ -492,9 +490,9 @@ private void processTuple(Table table, String line, String userId, Map klass = bean.getClass();
- Map getters = eiHandler.getGetters(klass);
- Set updaters = eiHandler.getSettersForUpdate(klass).keySet();
- for (Field f : eiHandler.getFields(klass)) {
+ Map