some refactoring for NonReferencedObjectsDeleter
authorAndreas Müller <a.mueller@bgbm.org>
Thu, 4 Nov 2021 09:11:26 +0000 (10:11 +0100)
committerAndreas Müller <a.mueller@bgbm.org>
Thu, 4 Nov 2021 09:11:26 +0000 (10:11 +0100)
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/operation/NonReferencedObjectsDeleter.java
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/operation/NonReferencedObjectsDeleterConfigurator.java

index 4483d42c2a01f1b8fe3ff5f54c34b09559008dde..963fb8a3f296669fad3341ef8e0741e7b3b57690 100644 (file)
@@ -16,6 +16,8 @@ import org.springframework.stereotype.Component;
 import eu.etaxonomy.cdm.api.service.DeleteResult;
 import eu.etaxonomy.cdm.io.common.CdmImportBase;
 import eu.etaxonomy.cdm.io.common.DefaultImportState;
+import eu.etaxonomy.cdm.model.agent.Person;
+import eu.etaxonomy.cdm.model.agent.Team;
 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
 import eu.etaxonomy.cdm.model.reference.Reference;
@@ -33,61 +35,84 @@ public class NonReferencedObjectsDeleter extends CdmImportBase<NonReferencedObje
     @Override
        protected void doInvoke(DefaultImportState<NonReferencedObjectsDeleterConfigurator> state) {
 
-        List<OrderHint> orderHint = new ArrayList<>();
-        orderHint.add(OrderHint.ORDER_BY_ID);
-
-               if (state.getConfig().isDoAuthors()){
-                       List<TeamOrPersonBase> authors =getAgentService().list(TeamOrPersonBase.class, null, null, orderHint, null);
-
-                       int deleted = 0;
-                       System.out.println("There are " + authors.size() + " authors");
-                       for (TeamOrPersonBase<?> author: authors){
-                               long refObjects = getCommonService().getReferencingObjectsCount(author);
-                               if (refObjects == 0) {
-                                   DeleteResult result = getAgentService().delete(author);
-                                       deleted++;
-                                       if (!result.isOk()){
-                                               System.out.println("Author " + author.getTitleCache() + " with id " + author.getId() + " could not be deleted.");
-                                               result = null;
-                                       }else{
-                                           System.out.println("Deleted: " + author.getTitleCache() + "; id = " + author.getId());
-                                       }
-                               }
-                       }
-                       System.out.println(deleted + " authors are deleted.");
-               }
-
-               List<String> propertyPath = new ArrayList<>();
-               propertyPath.add("sources.citation");
-               propertyPath.add("createdBy");
-               if (state.getConfig().isDoReferences()){
-                       List<Reference> references =getReferenceService().list(Reference.class, null, null, orderHint, propertyPath);
-
-                       int deleted = 0;
-                       System.out.println("There are " + references.size() + " references");
-                       for (Reference ref: references){
-                               long refObjects = getCommonService().getReferencingObjectsCount(ref);
-                               if (refObjects == 0) {
-                                   if (isIgnore(state, ref)){
-                                       System.out.println("Ignore: " + ref.getId() + "\t" + ref.getType() + "\t" +ref.getTitleCache() + "\t" + ref.getCreated()+ "\t" +
-                                               (ref.getCreatedBy() == null? "" : ref.getCreatedBy().getUsername()) + "\t" +
-                                               ref.getUpdated() + "\t" +  getSources(ref));
-                                   }else{
-                                       DeleteResult result = getReferenceService().delete(ref);
-                                       deleted++;
-                                       if (!result.isOk()){
-                                           System.out.println("Reference " + ref.getTitle() + " with id " + ref.getId() + " could not be deleted.");
-                                           result = null;
-                                       }else{
-//                                         System.out.println("Deleted: " + ref.getTitleCache() + "; id = " + ref.getId());
-                                       }
-                                   }
-                               }
-                       }
-                       System.out.println(deleted + " references are deleted.");
-               }
+        doReferences(state);
+        doTeams(state);
+        doPersons(state);
        }
 
+    private void doPersons(DefaultImportState<NonReferencedObjectsDeleterConfigurator> state) {
+        doAgents(state, Person.class, "Peson");
+    }
+
+    private void doTeams(DefaultImportState<NonReferencedObjectsDeleterConfigurator> state) {
+        doAgents(state, Team.class, "Team");
+    }
+
+    private void doAgents(DefaultImportState<NonReferencedObjectsDeleterConfigurator> state, Class<? extends TeamOrPersonBase<?>> clazz, String label) {
+
+        List<? extends TeamOrPersonBase<?>> authors =getAgentService().list(clazz, null, null, getOrderHint(), null);
+
+        int deleted = 0;
+        System.out.println("There are " + authors.size() + " " + label + "s.");
+        for (TeamOrPersonBase<?> author: authors){
+            long refObjectsCount = getCommonService().getReferencingObjectsCount(author);
+            if (refObjectsCount == 0) {
+                if (!state.getConfig().isDoOnlyReport()){
+                    DeleteResult result = getAgentService().delete(author);
+                    if (!result.isOk()){
+                        System.out.println(label + " " + author.getTitleCache() + " with id " + author.getId() + " could not be deleted.");
+                        result = null;
+                    }else{
+                        deleted++;
+                        System.out.println("Deleted: " + author.getTitleCache() + "; id = " + author.getId());
+                    }
+                }else{
+                    System.out.println(label + " to delete: " + author.getTitleCache() + "; id = " + author.getId());
+                }
+            }
+        }
+        System.out.println(deleted + " authors are deleted.");
+    }
+
+
+    private void doReferences(DefaultImportState<NonReferencedObjectsDeleterConfigurator> state) {
+        String label = "reference";
+        if (state.getConfig().isDoReferences()){
+            List<String> propertyPath = new ArrayList<>();
+            propertyPath.add("sources.citation");
+            propertyPath.add("createdBy");
+
+            List<Reference> references =getReferenceService().list(Reference.class, null, null, getOrderHint(), propertyPath);
+
+            int deleted = 0;
+            System.out.println("There are " + references.size() + " " + label + "s");
+            for (Reference ref: references){
+                long refObjects = getCommonService().getReferencingObjectsCount(ref);
+                if (refObjects == 0) {
+                    if (isIgnore(state, ref)){
+                        System.out.println("Ignore: " + ref.getId() + "\t" + ref.getType() + "\t" +ref.getTitleCache() + "\t" + ref.getCreated()+ "\t" +
+                                (ref.getCreatedBy() == null? "" : ref.getCreatedBy().getUsername()) + "\t" +
+                                ref.getUpdated() + "\t" +  getSources(ref));
+                    }else{
+                        if (!state.getConfig().isDoOnlyReport()){
+                            DeleteResult result = getReferenceService().delete(ref);
+                            if (!result.isOk()){
+                                System.out.println(label + " " + ref.getTitle() + " with id " + ref.getId() + " could not be deleted.");
+                                result = null;
+                            }else{
+                                deleted++;
+                                //System.out.println("Deleted: " + ref.getTitleCache() + "; id = " + ref.getId());
+                            }
+                        }else{
+                            System.out.println(label + " to delete: " + ref.getTitleCache() + "; id = " + ref.getId());
+                        }
+                    }
+                }
+            }
+            System.out.println(deleted + " " + label + "s are deleted.");
+        }
+    }
+
     private boolean isIgnore(DefaultImportState<NonReferencedObjectsDeleterConfigurator> state, Reference ref) {
         if (state.getConfig().isKeepReferencesWithTitle() && isNotBlank(ref.getTitle())
                 || state.getConfig().isKeepRisSources() && hasRISSource(ref)){
@@ -115,6 +140,12 @@ public class NonReferencedObjectsDeleter extends CdmImportBase<NonReferencedObje
         return false;
     }
 
+    private List<OrderHint> getOrderHint() {
+        List<OrderHint> orderHint = new ArrayList<>();
+        orderHint.add(OrderHint.ORDER_BY_ID);
+        return orderHint;
+    }
+
     @Override
        protected boolean doCheck(DefaultImportState<NonReferencedObjectsDeleterConfigurator> state) {
                return true;
index 938403bc9cf8f3fc7d7a3b98fe4a4f556940f68b..b3b2764f7cfeb0b02ed51472a65dc13163be680b 100644 (file)
@@ -35,6 +35,9 @@ public class NonReferencedObjectsDeleterConfigurator
        private boolean isKeepReferencesWithTitle = false;
        private boolean isKeepRisSources = false;
 
+       //if true, records are not deleted but only reported (later this will be part of the analyzing step
+       private boolean doOnlyReport = false;
+
        public NonReferencedObjectsDeleterConfigurator() {
                super(null);
        }
@@ -93,6 +96,14 @@ public class NonReferencedObjectsDeleterConfigurator
         this.isKeepRisSources = isKeepRisSources;
     }
 
+    //doOnlyReport
+    public boolean isDoOnlyReport() {
+        return doOnlyReport;
+    }
+    public void setDoOnlyReport(boolean doOnlyReport) {
+        this.doOnlyReport = doOnlyReport;
+    }
+
     @Override
     public Reference getSourceReference() {
         return null;