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 eklass = (Class) klass; - String classComment = eiHandler.getClassComment(eklass); + Class 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

"); - 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 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 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 con = eiHandler.getConstructor(klass); + Constructor 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 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 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 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 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) field.getType()).getBytes()); + EntityInfoHandler.getExportNull((Class) 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 originBean; + private final Class originBean; public Relationship(Class originBean, Field field, Class 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 entity : entities) { - entityNames.add(entity.getSimpleName()); - exportEntityNames.add(entity.getSimpleName()); - } - alphabeticEntityNames = new ArrayList<>(entityNames); - Collections.sort(alphabeticEntityNames); - } + public static Class getClass(String tableName) throws IcatException { + Class entityClass = ENTITY_NAME_MAP.get(tableName); - private final static Comparator 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 objectClass) throws IcatException { + private static PrivateEntityInfo buildEi(Class objectClass) { logger.debug("Building PrivateEntityInfo for " + objectClass); List fields = new ArrayList(); Class cobj = objectClass; @@ -292,7 +269,7 @@ private PrivateEntityInfo buildEi(Class 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 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 objectClass) t if (EntityBaseBean.class.isAssignableFrom(argc)) { @SuppressWarnings("unchecked") final Class argc2 = (Class) 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 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 argc2 = (Class) field.getType(); @@ -408,11 +371,11 @@ private PrivateEntityInfo buildEi(Class 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 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 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 objectClass) t Constructor 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 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 objectClass) t relInKey, hasLuceneDoc); } + private static PrivateEntityInfo getPrivateEntityInfo(Class 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 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 objectClass) { + return getPrivateEntityInfo(objectClass).attributes; } - public String getClassComment(Class 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 objectClass) { + return getPrivateEntityInfo(objectClass).classComment; } - public List getConstraintFields(Class 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 objectClass) { + return getPrivateEntityInfo(objectClass).constraintFields; } - public Constructor getConstructor(Class 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 getConstructor(Class objectClass) { + return getPrivateEntityInfo(objectClass).constructor; } - @SuppressWarnings("unchecked") - public EntityInfo getEntityInfo(String beanName) throws IcatException { - Class beanClass; - try { - beanClass = (Class) 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 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 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 objectClass) { + return getPrivateEntityInfo(objectClass).exportHeader; } - public String getExportHeaderAll(Class 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 objectClass) { + return getPrivateEntityInfo(objectClass).exportHeaderAll; } - public String getExportNull(Class 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 objectClass) { + return getPrivateEntityInfo(objectClass).exportNull; } - public Map getFieldComments(Class 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 objectClass) { + return getPrivateEntityInfo(objectClass).fieldComments; } /** * Returns all user settable fields (not id, createId, modId, createTime nor * modTime */ - public List getFields(Class 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 objectClass) { + return getPrivateEntityInfo(objectClass).fields; } - public Map getFieldsByName(Class 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 objectClass) { + return getPrivateEntityInfo(objectClass).fieldsByName; } /** * Map from field to getter for all fields */ - public Map getGetters(Class 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 objectClass) { + return getPrivateEntityInfo(objectClass).getters; } /** * Map from field name to getter for all fields */ - public Map getGettersFromName(Class 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 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 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 objectClass) { + return getPrivateEntityInfo(objectClass).notNullableFields; } - public Set getOnes(Class 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 objectClass) { + return getPrivateEntityInfo(objectClass).ones; } - public Set getRelatedEntities(Class 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 objectClass) { + return getPrivateEntityInfo(objectClass).relatedEntities; } /** @@ -890,92 +733,43 @@ public Set getRelatedEntities(Class obje * * @throws IcatException */ - public Map getRelationshipsByName(Class 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 objectClass) { + return getPrivateEntityInfo(objectClass).relationshipsByName; } - public Set getRelInKey(Class 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 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 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 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 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 objectClass) { + return getPrivateEntityInfo(objectClass).updaters; } /** * Returns all string fields except for createId and modId */ - public Map getStringFields(Class 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 objectClass) { + return getPrivateEntityInfo(objectClass).stringFields; } /** Return true if getDoc() method exists else false */ - public boolean hasLuceneDoc(Class 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 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 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) 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 getters = EntityInfoHandler.getGetters(klass); + Set updaters = EntityInfoHandler.getSettersForUpdate(klass).keySet(); + for (Field f : EntityInfoHandler.getFields(klass)) { if (updaters.contains(f)) { if (EntityBaseBean.class.isAssignableFrom(f.getType())) { EntityBaseBean beanField = (EntityBaseBean) getters.get(f).invoke(bean); diff --git a/src/main/java/org/icatproject/core/manager/PropertyHandler.java b/src/main/java/org/icatproject/core/manager/PropertyHandler.java index 884b0092c..9bafe275c 100644 --- a/src/main/java/org/icatproject/core/manager/PropertyHandler.java +++ b/src/main/java/org/icatproject/core/manager/PropertyHandler.java @@ -403,10 +403,9 @@ private void init() { String notificationList = props.getString(key); formattedProps.add(key + " " + notificationList); - EntityInfoHandler ei = EntityInfoHandler.getInstance(); for (String entity : notificationList.split("\\s+")) { try { - ei.getEntityInfo(entity); + EntityInfoHandler.getEntityInfo(entity); } catch (IcatException e) { String msg = "Value '" + entity + "' specified in 'notification.list' is not an ICAT entity"; logger.error(fatal, msg); diff --git a/src/main/java/org/icatproject/core/manager/importParser/AttWithOptParenList.java b/src/main/java/org/icatproject/core/manager/importParser/AttWithOptParenList.java index f97462a8e..d57a4d8f3 100644 --- a/src/main/java/org/icatproject/core/manager/importParser/AttWithOptParenList.java +++ b/src/main/java/org/icatproject/core/manager/importParser/AttWithOptParenList.java @@ -21,7 +21,6 @@ public class AttWithOptParenList { private String joins = ""; private List wheres; private List attributes; - private final static EntityInfoHandler eiHandler = EntityInfoHandler.getInstance(); @SuppressWarnings("unchecked") public AttWithOptParenList(Input input, Class entityClass, @@ -38,7 +37,7 @@ public AttWithOptParenList(Input input, Class entityClass, if (nextT == Token.Type.OPENPAREN) { entityClass = (Class) field.getType(); parenAttList = new ParenAttList(input, entityClass, - eiHandler.getFieldsByName(entityClass), eiHandler.getSetters(entityClass), + EntityInfoHandler.getFieldsByName(entityClass), EntityInfoHandler.getSetters(entityClass), parentName + relName); joins = " JOIN " + parentName + "." + attribute + " " + parentName + relName + parenAttList.getJoins(); diff --git a/src/main/java/org/icatproject/core/manager/importParser/Table.java b/src/main/java/org/icatproject/core/manager/importParser/Table.java index 42c293ae1..481d6bd2a 100644 --- a/src/main/java/org/icatproject/core/manager/importParser/Table.java +++ b/src/main/java/org/icatproject/core/manager/importParser/Table.java @@ -17,14 +17,13 @@ public class Table { private String name; private List tableFields = new ArrayList<>(); - private Class tableClass; - private final static EntityInfoHandler eiHandler = EntityInfoHandler.getInstance(); + private Class tableClass; public Table(Input input) throws ParserException, IcatException { name = input.consume(Token.Type.NAME).getValue(); tableClass = EntityInfoHandler.getClass(name); - Map fieldsByName = eiHandler.getFieldsByName(tableClass); - Map setters = eiHandler.getSetters(tableClass); + Map fieldsByName = EntityInfoHandler.getFieldsByName(tableClass); + Map setters = EntityInfoHandler.getSetters(tableClass); input.consume(Token.Type.OPENPAREN); while (true) { diff --git a/src/main/java/org/icatproject/core/manager/importParser/TableField.java b/src/main/java/org/icatproject/core/manager/importParser/TableField.java index eb9a47002..6dedaf2a4 100644 --- a/src/main/java/org/icatproject/core/manager/importParser/TableField.java +++ b/src/main/java/org/icatproject/core/manager/importParser/TableField.java @@ -26,7 +26,6 @@ public class TableField { private String jpql; private List attributes; private boolean qmark; - private final static EntityInfoHandler eiHandler = EntityInfoHandler.getInstance(); private static Map systemSetters = new HashMap<>(); @@ -46,7 +45,7 @@ public class TableField { } @SuppressWarnings("unchecked") - public TableField(Input input, Class tableClass, + public TableField(Input input, Class tableClass, Map fieldsByName, Map setters) throws ParserException, IcatException { Token next = input.consume(Token.Type.NAME, Token.Type.QMARK); @@ -84,7 +83,7 @@ public TableField(Input input, Class tableClass, } else { Class entityClass = (Class) field.getType(); parenAttlist = new ParenAttList(input, entityClass, - eiHandler.getFieldsByName(entityClass), eiHandler.getSetters(tableClass), + EntityInfoHandler.getFieldsByName(entityClass), EntityInfoHandler.getSetters(tableClass), "n0"); StringBuilder sb = new StringBuilder("SELECT n0 FROM " + entityClass.getSimpleName() + " n0" + parenAttlist.getJoins()); diff --git a/src/main/java/org/icatproject/core/oldparser/DagHandler.java b/src/main/java/org/icatproject/core/oldparser/DagHandler.java index 31c15c4c0..cde78ff69 100644 --- a/src/main/java/org/icatproject/core/oldparser/DagHandler.java +++ b/src/main/java/org/icatproject/core/oldparser/DagHandler.java @@ -15,8 +15,6 @@ */ public class DagHandler { - private static EntityInfoHandler pkHandler = EntityInfoHandler.getInstance(); - /** * A nested structure with a bean a relationship and the set of steps. It is a set because there * may be a need to follow more than one chain of entities. @@ -114,7 +112,7 @@ private static Set follow(Class predecessor, Class from, Set> allBeans, Set> used) throws IcatException { Set steps = new HashSet(); - Set navto = pkHandler.getRelatedEntities(from); + Set navto = EntityInfoHandler.getRelatedEntities(from); for (Relationship relationship : navto) { Class bean = relationship.getDestinationBean(); if (allBeans.contains(bean) && !bean.equals(predecessor)) { @@ -167,7 +165,7 @@ private static Set followIncludes(Class predeces Class from, Set> allBeans, Set> used, boolean followCascades) throws IcatException { Set steps = new HashSet(); - Set navto = pkHandler.getRelatedEntities(from); + Set navto = EntityInfoHandler.getRelatedEntities(from); for (Relationship relationship : navto) { if (!relationship.isCollection() || followCascades) { Class bean = relationship.getDestinationBean(); diff --git a/src/main/java/org/icatproject/core/oldparser/OldInclude.java b/src/main/java/org/icatproject/core/oldparser/OldInclude.java index 19866bfd2..2d7d5bd7c 100644 --- a/src/main/java/org/icatproject/core/oldparser/OldInclude.java +++ b/src/main/java/org/icatproject/core/oldparser/OldInclude.java @@ -22,8 +22,6 @@ private enum Position { FIRST, LOWER }; - private static EntityInfoHandler eiHandler = EntityInfoHandler.getInstance(); - private Set> includes = new HashSet>(); static Logger logger = LoggerFactory.getLogger(OldInclude.class); @@ -72,7 +70,7 @@ private static String addIncludes(StringBuilder sb, Class relationships = eiHandler.getRelatedEntities(entityClass); + Set relationships = EntityInfoHandler.getRelatedEntities(entityClass); for (Relationship r : relationships) { if (!r.isCollection() || followCascades == FollowCascades.TRUE) { Class bean = r.getDestinationBean(); diff --git a/src/main/java/org/icatproject/core/parser/FromClause.java b/src/main/java/org/icatproject/core/parser/FromClause.java index 8e734e049..a798ecdd7 100644 --- a/src/main/java/org/icatproject/core/parser/FromClause.java +++ b/src/main/java/org/icatproject/core/parser/FromClause.java @@ -17,8 +17,6 @@ public class FromClause { - private static final EntityInfoHandler eiHandler = EntityInfoHandler.getInstance(); - private String clause; private Map> authzMap = new HashMap<>(); @@ -50,7 +48,7 @@ public FromClause(Input input, Set idPaths) throws ParserException, Icat String val = t.getValue(); sb.append(" " + val); if (t.getType() == Token.Type.NAME) { - if (EntityInfoHandler.getAlphabeticEntityNames().contains(val)) { + if (EntityInfoHandler.getEntityNamesList().contains(val)) { currentEntity = EntityInfoHandler.getClass(val); } else { int dot = val.indexOf('.'); @@ -72,7 +70,7 @@ public FromClause(Input input, Set idPaths) throws ParserException, Icat int dotn = val.indexOf(".", dot + 1); String relPath = dotn < 0 ? val.substring(dot + 1) : val.substring(dot + 1, dotn); - Relationship r = eiHandler.getRelationshipsByName(currentEntity).get(relPath); + Relationship r = EntityInfoHandler.getRelationshipsByName(currentEntity).get(relPath); if (r == null) { throw new ParserException( currentEntity.getSimpleName() + " has no relationship field " + relPath); @@ -107,7 +105,7 @@ public FromClause(Input input, Set idPaths) throws ParserException, Icat int dotn = idPath.indexOf(".", dot + 1); String relPath = dotn < 0 ? idPath.substring(dot + 1) : idPath.substring(dot + 1, dotn); - Field f = eiHandler.getFieldsByName(bean).get(relPath); + Field f = EntityInfoHandler.getFieldsByName(bean).get(relPath); if (f == null) { throw new ParserException(bean.getSimpleName() + " has no field " + relPath); } diff --git a/src/main/java/org/icatproject/core/parser/IncludeClause.java b/src/main/java/org/icatproject/core/parser/IncludeClause.java index 94a626a76..53c3ebe3b 100644 --- a/src/main/java/org/icatproject/core/parser/IncludeClause.java +++ b/src/main/java/org/icatproject/core/parser/IncludeClause.java @@ -27,7 +27,7 @@ public class Step { public Step(int hereVarNum, String fieldName, int thereVarNum, GateKeeper gateKeeper, Set keys) throws IcatException, ParserException { - for (Relationship r : eiHandler.getRelatedEntities(types.get(hereVarNum))) { + for (Relationship r : EntityInfoHandler.getRelatedEntities(types.get(hereVarNum))) { if (r.getField().getName().equals(fieldName)) { types.put(thereVarNum, r.getDestinationBean()); relationship = r; @@ -67,8 +67,6 @@ public boolean isAllowed() { } - private static final EntityInfoHandler eiHandler = EntityInfoHandler.getInstance(); - static Logger logger = LoggerFactory.getLogger(IncludeClause.class); private boolean one; diff --git a/src/main/java/org/icatproject/core/parser/RuleWhat.java b/src/main/java/org/icatproject/core/parser/RuleWhat.java index d7d1682c2..d8f2be0f8 100644 --- a/src/main/java/org/icatproject/core/parser/RuleWhat.java +++ b/src/main/java/org/icatproject/core/parser/RuleWhat.java @@ -25,8 +25,6 @@ public class RuleWhat { private Pattern idPattern = Pattern.compile("^[a-zA-Z_$]([\\w_$])*$"); - EntityInfoHandler ei = EntityInfoHandler.getInstance(); - public RuleWhat(String query) throws ParserException, IcatException { List tokens; try { @@ -56,7 +54,7 @@ public RuleWhat(String query) throws ParserException, IcatException { if (offset > 0) { Class bean = fromClause.getAuthzMap().get(idVar + ".id"); - if (ei.getFieldsByName(bean).get(attribute) == null) { + if (EntityInfoHandler.getFieldsByName(bean).get(attribute) == null) { throw new ParserException(bean + " does not have attribute " + attribute); } } diff --git a/src/main/java/org/icatproject/exposed/ICATRest.java b/src/main/java/org/icatproject/exposed/ICATRest.java index cbd416c10..4c551b8f2 100644 --- a/src/main/java/org/icatproject/exposed/ICATRest.java +++ b/src/main/java/org/icatproject/exposed/ICATRest.java @@ -111,8 +111,6 @@ private static Date dec(String value) throws java.text.ParseException { } } - private static EntityInfoHandler eiHandler = EntityInfoHandler.getInstance(); - private Map authPlugins; @EJB @@ -303,7 +301,7 @@ private EntityBaseBean getOne(JsonObject entity, int offset) throws IcatExceptio } Entry entry = entity.entrySet().iterator().next(); String beanName = entry.getKey(); - Class klass = EntityInfoHandler.getClass(beanName); + Class klass = EntityInfoHandler.getClass(beanName); JsonObject contents = (JsonObject) entry.getValue(); EntityBaseBean bean = null; @@ -724,11 +722,11 @@ private void jsonise(EntityBaseBean bean, JsonGenerator gen) throws IcatExceptio } Class klass = bean.getClass(); - Map getters = eiHandler.getGetters(klass); - Set atts = eiHandler.getAttributes(klass); - Set updaters = eiHandler.getSettersForUpdate(klass).keySet(); + Map getters = EntityInfoHandler.getGetters(klass); + Set atts = EntityInfoHandler.getAttributes(klass); + Set updaters = EntityInfoHandler.getSettersForUpdate(klass).keySet(); - for (Field field : eiHandler.getFields(klass)) { + for (Field field : EntityInfoHandler.getFields(klass)) { Object value = null; try { value = getters.get(field).invoke(bean); diff --git a/src/test/java/org/icatproject/core/manager/TestEntityInfo.java b/src/test/java/org/icatproject/core/manager/TestEntityInfo.java index d43b6dd6a..b9f28e3f0 100644 --- a/src/test/java/org/icatproject/core/manager/TestEntityInfo.java +++ b/src/test/java/org/icatproject/core/manager/TestEntityInfo.java @@ -13,7 +13,6 @@ import java.util.Map.Entry; import java.util.Set; -import org.icatproject.core.Constants; import org.icatproject.core.IcatException; import org.icatproject.core.entity.DataCollection; import org.icatproject.core.entity.DataCollectionDatafile; @@ -37,11 +36,9 @@ public class TestEntityInfo { - private static EntityInfoHandler eiHandler = EntityInfoHandler.getInstance(); - @Test(expected = IcatException.class) public void testBadname() throws Exception { - eiHandler.getEntityInfo("Fred"); + EntityInfoHandler.getEntityInfo("Fred"); } @Test @@ -50,12 +47,11 @@ public void testHasLuceneDoc() throws Exception { "InvestigationParameter", "DatasetParameter", "DatafileParameter", "InvestigationUser", "Sample")); for (String beanName : EntityInfoHandler.getEntityNamesList()) { @SuppressWarnings("unchecked") - Class bean = (Class) Class - .forName(Constants.ENTITY_PREFIX + beanName); + Class bean = EntityInfoHandler.getClass(beanName); if (docdbeans.contains(beanName)) { - assertTrue(eiHandler.hasLuceneDoc(bean)); + assertTrue(EntityInfoHandler.hasLuceneDoc(bean)); } else { - assertFalse(eiHandler.hasLuceneDoc(bean)); + assertFalse(EntityInfoHandler.hasLuceneDoc(bean)); } } } @@ -63,38 +59,38 @@ public void testHasLuceneDoc() throws Exception { @Test public void testExportHeaders() throws Exception { assertEquals("Facility(daysUntilRelease:0,description:1,fullName:2,name:3,url:4)", - eiHandler.getExportHeader(Facility.class)); + EntityInfoHandler.getExportHeader(Facility.class)); assertEquals("InvestigationType(description:0,facility(name:1),name:2)", - eiHandler.getExportHeader(InvestigationType.class)); + EntityInfoHandler.getExportHeader(InvestigationType.class)); assertEquals( "Investigation(doi:0,endDate:1,facility(name:2),fileCount:3,fileSize:4,name:5,releaseDate:6," + "startDate:7,summary:8,title:9,type(facility(name:10),name:11),visitId:12)", - eiHandler.getExportHeader(Investigation.class)); + EntityInfoHandler.getExportHeader(Investigation.class)); assertEquals( "Dataset(complete:0,description:1,doi:2,endDate:3,fileCount:4,fileSize:5,investigation(facility(name:6)," + "name:7,visitId:8),location:9,name:10,sample(investigation(facility(name:11)," + "name:12,visitId:13),name:14),startDate:15,type(facility(name:16),name:17))", - eiHandler.getExportHeader(Dataset.class)); - assertEquals("DataCollection(?:0,doi:1)", eiHandler.getExportHeader(DataCollection.class)); + EntityInfoHandler.getExportHeader(Dataset.class)); + assertEquals("DataCollection(?:0,doi:1)", EntityInfoHandler.getExportHeader(DataCollection.class)); assertEquals( "Job(?:0,application(facility(name:1),name:2,version:3),arguments:4,inputDataCollection(?:5),outputDataCollection(?:6))", - eiHandler.getExportHeader(Job.class)); + EntityInfoHandler.getExportHeader(Job.class)); assertEquals( "Study(?:0,description:1,endDate:2,name:3,pid:4,startDate:5,status:6,user(name:7))", - eiHandler.getExportHeader(Study.class)); + EntityInfoHandler.getExportHeader(Study.class)); assertEquals( "DatasetParameter(dataset(investigation(facility(name:0),name:1,visitId:2),name:3)," + "dateTimeValue:4,error:5,numericValue:6,rangeBottom:7,rangeTop:8,stringValue:9," + "type(facility(name:10),name:11,units:12))", - eiHandler.getExportHeader(DatasetParameter.class)); + EntityInfoHandler.getExportHeader(DatasetParameter.class)); assertEquals("ParameterType(applicableToDataCollection:0,applicableToDatafile:1,applicableToDataset:2," + "applicableToInvestigation:3,applicableToSample:4,description:5,enforced:6,facility(name:7)," + "maximumNumericValue:8,minimumNumericValue:9,name:10,pid:11,units:12,unitsFullName:13," - + "valueType:14,verified:15)", eiHandler.getExportHeader(ParameterType.class)); - assertEquals("DataCollection(?:0,doi:1)", eiHandler.getExportHeader(DataCollection.class)); - assertEquals("Rule(?:0,crudFlags:1,grouping(name:2),what:3)", eiHandler.getExportHeader(Rule.class)); + + "valueType:14,verified:15)", EntityInfoHandler.getExportHeader(ParameterType.class)); + assertEquals("DataCollection(?:0,doi:1)", EntityInfoHandler.getExportHeader(DataCollection.class)); + assertEquals("Rule(?:0,crudFlags:1,grouping(name:2),what:3)", EntityInfoHandler.getExportHeader(Rule.class)); assertEquals( "DataCollectionDatafile(dataCollection(?:0),datafile(dataset(investigation(facility(name:1),name:2,visitId:3),name:4),name:5))", - eiHandler.getExportHeader(DataCollectionDatafile.class)); + EntityInfoHandler.getExportHeader(DataCollectionDatafile.class)); } @Test @@ -102,38 +98,38 @@ public void testExportHeaderAll() throws Exception { assertEquals( "Facility(createId:0,createTime:1,modId:2," + "modTime:3,daysUntilRelease:4,description:5,fullName:6,name:7,url:8)", - eiHandler.getExportHeaderAll(Facility.class)); + EntityInfoHandler.getExportHeaderAll(Facility.class)); assertEquals( "InvestigationType(createId:0,createTime:1,modId:2," + "modTime:3,description:4,facility(name:5),name:6)", - eiHandler.getExportHeaderAll(InvestigationType.class)); + EntityInfoHandler.getExportHeaderAll(InvestigationType.class)); assertEquals( "Investigation(createId:0,createTime:1,modId:2,modTime:3," + "doi:4,endDate:5,facility(name:6),fileCount:7,fileSize:8,name:9,releaseDate:10," + "startDate:11,summary:12,title:13,type(facility(name:14),name:15),visitId:16)", - eiHandler.getExportHeaderAll(Investigation.class)); + EntityInfoHandler.getExportHeaderAll(Investigation.class)); assertEquals( "Dataset(createId:0,createTime:1,modId:2,modTime:3," + "complete:4,description:5,doi:6,endDate:7,fileCount:8,fileSize:9," + "investigation(facility(name:10),name:11,visitId:12),location:13,name:14," + "sample(investigation(facility(name:15),name:16,visitId:17)," + "name:18),startDate:19,type(facility(name:20),name:21))", - eiHandler.getExportHeaderAll(Dataset.class)); + EntityInfoHandler.getExportHeaderAll(Dataset.class)); assertEquals("DataCollection(?:0,createId:1,createTime:2,modId:3,modTime:4,doi:5)", - eiHandler.getExportHeaderAll(DataCollection.class)); + EntityInfoHandler.getExportHeaderAll(DataCollection.class)); assertEquals( "Job(?:0,createId:1,createTime:2,modId:3,modTime:4," + "application(facility(name:5),name:6,version:7),arguments:8," + "inputDataCollection(?:9),outputDataCollection(?:10))", - eiHandler.getExportHeaderAll(Job.class)); + EntityInfoHandler.getExportHeaderAll(Job.class)); assertEquals( "Study(?:0,description:1,endDate:2,name:3,pid:4,startDate:5,status:6,user(name:7))", - eiHandler.getExportHeader(Study.class)); + EntityInfoHandler.getExportHeader(Study.class)); assertEquals( "DatasetParameter(createId:0,createTime:1,modId:2,modTime:3," + "dataset(investigation(facility(name:4),name:5,visitId:6),name:7)," + "dateTimeValue:8,error:9,numericValue:10,rangeBottom:11," + "rangeTop:12,stringValue:13,type(facility(name:14),name:15,units:16))", - eiHandler.getExportHeaderAll(DatasetParameter.class)); + EntityInfoHandler.getExportHeaderAll(DatasetParameter.class)); assertEquals( "ParameterType(createId:0,createTime:1,modId:2,modTime:3," + "applicableToDataCollection:4,applicableToDatafile:5," @@ -141,27 +137,27 @@ public void testExportHeaderAll() throws Exception { + "applicableToSample:8,description:9,enforced:10,facility(name:11)," + "maximumNumericValue:12,minimumNumericValue:13,name:14,pid:15," + "units:16,unitsFullName:17,valueType:18,verified:19)", - eiHandler.getExportHeaderAll(ParameterType.class)); - assertEquals("DataCollection(?:0,doi:1)", eiHandler.getExportHeader(DataCollection.class)); + EntityInfoHandler.getExportHeaderAll(ParameterType.class)); + assertEquals("DataCollection(?:0,doi:1)", EntityInfoHandler.getExportHeader(DataCollection.class)); assertEquals("Rule(?:0,createId:1,createTime:2,modId:3,modTime:4," + "crudFlags:5,grouping(name:6),what:7)", - eiHandler.getExportHeaderAll(Rule.class)); + EntityInfoHandler.getExportHeaderAll(Rule.class)); assertEquals( "DataCollectionDatafile(createId:0,createTime:1,modId:2,modTime:3," + "dataCollection(?:4),datafile(dataset(investigation(facility(name:5)," + "name:6,visitId:7),name:8),name:9))", - eiHandler.getExportHeaderAll(DataCollectionDatafile.class)); + EntityInfoHandler.getExportHeaderAll(DataCollectionDatafile.class)); } @Test public void testExportNull() throws Exception { - assertEquals("NULL", eiHandler.getExportNull(Facility.class)); - assertEquals("NULL,NA", eiHandler.getExportNull(InvestigationType.class)); - assertEquals("NULL,NA,NA", eiHandler.getExportNull(Investigation.class)); - assertEquals("NULL,NA,NA,NA", eiHandler.getExportNull(Dataset.class)); - assertEquals("", eiHandler.getExportNull(DataCollection.class)); - assertEquals("", eiHandler.getExportNull(Job.class)); - assertEquals("NULL,NA,NA,NA,NA,NA,NA", eiHandler.getExportNull(DatasetParameter.class)); - assertEquals("NULL,NA,NA", eiHandler.getExportNull(ParameterType.class)); + assertEquals("NULL", EntityInfoHandler.getExportNull(Facility.class)); + assertEquals("NULL,NA", EntityInfoHandler.getExportNull(InvestigationType.class)); + assertEquals("NULL,NA,NA", EntityInfoHandler.getExportNull(Investigation.class)); + assertEquals("NULL,NA,NA,NA", EntityInfoHandler.getExportNull(Dataset.class)); + assertEquals("", EntityInfoHandler.getExportNull(DataCollection.class)); + assertEquals("", EntityInfoHandler.getExportNull(Job.class)); + assertEquals("NULL,NA,NA,NA,NA,NA,NA", EntityInfoHandler.getExportNull(DatasetParameter.class)); + assertEquals("NULL,NA,NA", EntityInfoHandler.getExportNull(ParameterType.class)); } @Test @@ -193,7 +189,7 @@ public void testFields() throws Exception { private void testField(String result, Class klass) throws Exception { String sep = ""; StringBuilder sb = new StringBuilder(); - for (Field f : eiHandler.getFields(klass)) { + for (Field f : EntityInfoHandler.getFields(klass)) { sb.append(sep + f.getName()); sep = ","; } @@ -211,7 +207,7 @@ public void testConstraints() throws Exception { } private void testConstraint(Class klass, String... name) throws Exception { - List result = eiHandler.getConstraintFields(klass); + List result = EntityInfoHandler.getConstraintFields(klass); if (name.length == 0) { assertEquals("One", 0, result.size()); } else { @@ -227,7 +223,7 @@ private void testConstraint(Class klass, String... nam @Test public void testFieldByName() throws Exception { - Map fields = eiHandler.getFieldsByName(EntityInfoHandler.getClass("DatafileParameter")); + Map fields = EntityInfoHandler.getFieldsByName(EntityInfoHandler.getClass("DatafileParameter")); assertEquals("protected java.lang.Long org.icatproject.core.entity.EntityBaseBean.id", fields.get("id").toString()); assertEquals( @@ -285,7 +281,7 @@ public void testRels() throws Exception { } private void testRel(Class klass, String... rels) throws Exception { - Set results = eiHandler.getRelatedEntities(klass); + Set results = EntityInfoHandler.getRelatedEntities(klass); Set rStrings = new HashSet(); for (Relationship rel : results) { rStrings.add(rel.toString()); @@ -294,7 +290,7 @@ private void testRel(Class klass, String... rels) thro for (String rel : rels) { assertTrue(klass.getSimpleName() + " value " + rel, rStrings.contains(rel)); } - Set> map = eiHandler.getRelationshipsByName(klass).entrySet(); + Set> map = EntityInfoHandler.getRelationshipsByName(klass).entrySet(); assertEquals(rels.length, map.size()); for (Entry e : map) { Relationship rel = e.getValue(); @@ -322,7 +318,7 @@ public void testOnes() throws Exception { } private void testOne(Class klass, String... rels) throws Exception { - Set results = eiHandler.getOnes(klass); + Set results = EntityInfoHandler.getOnes(klass); Set rStrings = new HashSet(); for (Relationship rel : results) { rStrings.add(rel.getDestinationBean().getSimpleName()); @@ -347,7 +343,7 @@ public void notNullableFields() throws Exception { } private void testNNF(Class klass, String... nnfs) throws Exception { - List results = eiHandler.getNotNullableFields(klass); + List results = EntityInfoHandler.getNotNullableFields(klass); Set rStrings = new HashSet(); for (Field field : results) { rStrings.add(field.getName()); @@ -373,7 +369,7 @@ public void stringFields() throws Exception { } private void testSF(Class klass, String... sfs) throws Exception { - Map results = eiHandler.getStringFields(klass); + Map results = EntityInfoHandler.getStringFields(klass); Set rStrings = new HashSet(); for (Entry entry : results.entrySet()) { rStrings.add(entry.getKey().getName() + " " + entry.getValue()); @@ -432,7 +428,7 @@ public void relInKey() throws Exception { } private void testRelInkey(Class klass, String... fieldNames) throws Exception { - Set results = eiHandler.getRelInKey(klass); + Set results = EntityInfoHandler.getRelInKey(klass); Set rStrings = new HashSet(); for (Field field : results) { rStrings.add(field.getName()); @@ -445,7 +441,7 @@ private void testRelInkey(Class klass, String... field } private void testGetters(Class klass, int count) throws Exception { - Map results = eiHandler.getGetters(klass); + Map results = EntityInfoHandler.getGetters(klass); assertEquals(klass.getSimpleName() + " count", count, results.size()); for (Entry entry : results.entrySet()) { String cap = entry.getKey().getName(); @@ -456,7 +452,7 @@ private void testGetters(Class klass, int count) throw } private void testSettersForUpdate(Class klass, int count) throws Exception { - Map results = eiHandler.getSettersForUpdate(klass); + Map results = EntityInfoHandler.getSettersForUpdate(klass); assertEquals(klass.getSimpleName() + " count", count, results.size()); for (Entry entry : results.entrySet()) { @@ -468,7 +464,7 @@ private void testSettersForUpdate(Class klass, int cou } private void testSetters(Class klass, int count) throws Exception { - Map results = eiHandler.getSetters(klass); + Map results = EntityInfoHandler.getSetters(klass); assertEquals(klass.getSimpleName() + " count", count, results.size()); for (Entry entry : results.entrySet()) { String cap = entry.getKey().getName(); diff --git a/src/test/java/org/icatproject/integration/TestWS.java b/src/test/java/org/icatproject/integration/TestWS.java index dde35597b..130e38193 100644 --- a/src/test/java/org/icatproject/integration/TestWS.java +++ b/src/test/java/org/icatproject/integration/TestWS.java @@ -239,7 +239,7 @@ public void publicStep() throws Exception { fail("Should have thrown exception"); } catch (IcatException_Exception e) { assertEquals(IcatExceptionType.BAD_PARAMETER, e.getFaultInfo().getType()); - assertEquals("Applicatio is not an EntityBaseBean", e.getMessage()); + assertEquals("Applicatio is not an ICAT entity", e.getMessage()); } ps = new PublicStep(); @@ -1052,7 +1052,7 @@ public void groupAuthz() throws Exception { session.addUserGroupMember("root", "root"); session.addUserGroupMember("root", "useroffice"); - for (String t : EntityInfoHandler.getAlphabeticEntityNames()) { + for (String t : EntityInfoHandler.getEntityNamesList()) { session.addRule("root", "SELECT x FROM " + t + " x", "CRUD"); session.addRule("notroot", "SELECT x FROM " + t + " x", "CRUD"); } @@ -1138,7 +1138,7 @@ public void gets() throws Exception { fail("No throw"); } catch (IcatException_Exception e) { assertEquals(IcatExceptionType.BAD_PARAMETER, e.getFaultInfo().getType()); - assertEquals("Investigator is not an EntityBaseBean", e.getMessage()); + assertEquals("Investigator is not an ICAT entity", e.getMessage()); } try { session.get("Dataset INCLUDE Investigation, Facility, Instrument", dsId); @@ -1508,7 +1508,7 @@ public void oldGets() throws Exception { fail("No throw"); } catch (IcatException_Exception e) { assertEquals(IcatExceptionType.BAD_PARAMETER, e.getFaultInfo().getType()); - assertEquals("Investigator is not an EntityBaseBean", e.getMessage()); + assertEquals("Investigator is not an ICAT entity", e.getMessage()); } try { session.get("Dataset INCLUDE Investigation, Facility, Instrument", dsId);
CardClassFieldCascadedDescription