Skip to content

Commit

Permalink
Merge pull request #323 from icatproject/entity-info-handler-static
Browse files Browse the repository at this point in the history
Fully-static EntityInfoHandler
  • Loading branch information
ajkyffin authored Mar 18, 2024
2 parents 6053c58 + c5ecf9d commit c855f7f
Show file tree
Hide file tree
Showing 21 changed files with 314 additions and 566 deletions.
20 changes: 8 additions & 12 deletions src/main/java/org/icatproject/core/DocGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -45,20 +43,18 @@ public static void main(String[] args) throws Exception {

for (String cname : cnames) {
out.print("<hr/><h2 id=\"" + cname + "\">" + cname + "</h2>");
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("<p>" + classComment + "</p>");
}

Set<Field> fields = eiHandler.getGetters(eklass).keySet();
Map<Field, String> fieldComments = eiHandler.getFieldComments(eklass);
Set<Field> notnullables = new HashSet<Field>(eiHandler.getNotNullableFields(eklass));
Map<Field, Integer> stringFields = eiHandler.getStringFields(eklass);
Set<Field> fields = new HashSet<Field>(EntityInfoHandler.getGetters(eklass).keySet());
Map<Field, String> fieldComments = EntityInfoHandler.getFieldComments(eklass);
Set<Field> notnullables = new HashSet<Field>(EntityInfoHandler.getNotNullableFields(eklass));
Map<Field, Integer> stringFields = EntityInfoHandler.getStringFields(eklass);

Iterator<Field> iter = fields.iterator();
while (iter.hasNext()) {
Expand All @@ -69,7 +65,7 @@ public static void main(String[] args) throws Exception {
}
}

List<Field> constraint = eiHandler.getConstraintFields(eklass);
List<Field> constraint = EntityInfoHandler.getConstraintFields(eklass);
if (!constraint.isEmpty()) {
out.print("<p><b>Uniqueness constraint</b> ");
first = true;
Expand All @@ -86,7 +82,7 @@ public static void main(String[] args) throws Exception {

out.println(
"<h3>Relationships</h3><table><tr><th>Card</th><th>Class</th><th>Field</th><th>Cascaded</th><th>Description</th></tr>");
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();
Expand Down
24 changes: 11 additions & 13 deletions src/main/java/org/icatproject/core/entity/EntityBaseBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<Relationship> rs = eiHandler.getRelatedEntities(klass);
Map<Field, Method> getters = eiHandler.getGetters(klass);
Set<Relationship> rs = EntityInfoHandler.getRelatedEntities(klass);
Map<Field, Method> getters = EntityInfoHandler.getGetters(klass);
for (Relationship r : rs) {
if (r.isCollection()) {
Method m = getters.get(r.getField());
Expand Down Expand Up @@ -147,9 +145,9 @@ public void collectIds(Map<String, Set<Long>> ids, boolean one, int hereVarNum,

ids.get(beanName).add(id);
try {
Map<Field, Method> getters = eiHandler.getGetters(klass);
Map<Field, Method> 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) {
Expand Down Expand Up @@ -307,8 +305,8 @@ public void preparePersist(String modId, EntityManager manager, GateKeeper gateK
}

Class<? extends EntityBaseBean> klass = this.getClass();
Set<Relationship> rs = eiHandler.getRelatedEntities(klass);
Map<Field, Method> getters = eiHandler.getGetters(klass);
Set<Relationship> rs = EntityInfoHandler.getRelatedEntities(klass);
Map<Field, Method> getters = EntityInfoHandler.getGetters(klass);
for (Relationship r : rs) {
if (r.isCollection()) {
Method m = getters.get(r.getField());
Expand Down Expand Up @@ -342,24 +340,24 @@ public EntityBaseBean pruned(boolean one, int hereVarNum, List<Step> 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<Field> atts = eiHandler.getAttributes(klass);
Map<Field, Method> getters = eiHandler.getGetters(klass);
Map<Field, Method> setters = eiHandler.getSettersForUpdate(klass);
Set<Field> atts = EntityInfoHandler.getAttributes(klass);
Map<Field, Method> getters = EntityInfoHandler.getGetters(klass);
Map<Field, Method> setters = EntityInfoHandler.getSettersForUpdate(klass);
for (Field att : atts) {
Object value = getters.get(att).invoke(this);
if (value != null) {
setters.get(att).invoke(clone, new Object[] { value });
}
}
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) {
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/icatproject/core/entity/PublicStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -64,7 +62,7 @@ public PublicStep() {

private void fixup(EntityManager manager, GateKeeper gateKeeper) throws IcatException {
Class<? extends EntityBaseBean> bean = EntityInfoHandler.getClass(origin);
Set<Relationship> rs = eiHandler.getRelatedEntities(bean);
Set<Relationship> rs = EntityInfoHandler.getRelatedEntities(bean);
boolean found = false;
for (Relationship r : rs) {
if (r.getField().getName().equals(field)) {
Expand Down
Loading

0 comments on commit c855f7f

Please sign in to comment.