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;
@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)){
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;