Project

General

Profile

Download (4.62 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.io.operation;
10

    
11
import java.util.ArrayList;
12
import java.util.List;
13

    
14
import org.springframework.stereotype.Component;
15

    
16
import eu.etaxonomy.cdm.api.service.DeleteResult;
17
import eu.etaxonomy.cdm.io.common.CdmImportBase;
18
import eu.etaxonomy.cdm.io.common.DefaultImportState;
19
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
20
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
21
import eu.etaxonomy.cdm.model.reference.Reference;
22
import eu.etaxonomy.cdm.persistence.query.OrderHint;
23

    
24
/**
25
 * @author k.luther
26
 * @since 2015
27
 */
28
@Component
29
public class NonReferencedObjectsDeleter extends CdmImportBase<NonReferencedObjectsDeleterConfigurator, DefaultImportState<NonReferencedObjectsDeleterConfigurator>> {
30

    
31
    private static final long serialVersionUID = -3514276133181062270L;
32

    
33
    @Override
34
	protected void doInvoke(DefaultImportState<NonReferencedObjectsDeleterConfigurator> state) {
35

    
36
        List<OrderHint> orderHint = new ArrayList<>();
37
        orderHint.add(OrderHint.ORDER_BY_ID);
38

    
39
		if (state.getConfig().isDoAuthors()){
40
			List<TeamOrPersonBase> authors =getAgentService().list(TeamOrPersonBase.class, null, null, orderHint, null);
41

    
42
			int deleted = 0;
43
			System.out.println("There are " + authors.size() + " authors");
44
			for (TeamOrPersonBase<?> author: authors){
45
				long refObjects = getCommonService().getReferencingObjectsCount(author);
46
				if (refObjects == 0) {
47
				    DeleteResult result = getAgentService().delete(author);
48
					deleted++;
49
					if (!result.isOk()){
50
						System.out.println("Author " + author.getTitleCache() + " with id " + author.getId() + " could not be deleted.");
51
						result = null;
52
					}else{
53
					    System.out.println("Deleted: " + author.getTitleCache() + "; id = " + author.getId());
54
					}
55
				}
56
			}
57
			System.out.println(deleted + " authors are deleted.");
58
		}
59

    
60
		List<String> propertyPath = new ArrayList<>();
61
		propertyPath.add("sources.citation");
62
		propertyPath.add("createdBy");
63
		if (state.getConfig().isDoReferences()){
64
			List<Reference> references =getReferenceService().list(Reference.class, null, null, orderHint, propertyPath);
65

    
66
			int deleted = 0;
67
			System.out.println("There are " + references.size() + " references");
68
			for (Reference ref: references){
69
				long refObjects = getCommonService().getReferencingObjectsCount(ref);
70
				if (refObjects == 0) {
71
				    if (isIgnore(state, ref)){
72
				        System.out.println("Ignore: " + ref.getId() + "\t" + ref.getType() + "\t" +ref.getTitleCache() + "\t" + ref.getCreated()+ "\t" +
73
				                (ref.getCreatedBy() == null? "" : ref.getCreatedBy().getUsername()) + "\t" +
74
				                ref.getUpdated() + "\t" +  getSources(ref));
75
				    }else{
76
				        DeleteResult result = getReferenceService().delete(ref);
77
				        deleted++;
78
				        if (!result.isOk()){
79
				            System.out.println("Reference " + ref.getTitle() + " with id " + ref.getId() + " could not be deleted.");
80
				            result = null;
81
				        }else{
82
//				            System.out.println("Deleted: " + ref.getTitleCache() + "; id = " + ref.getId());
83
				        }
84
				    }
85
				}
86
			}
87
			System.out.println(deleted + " references are deleted.");
88
		}
89
	}
90

    
91
    private boolean isIgnore(DefaultImportState<NonReferencedObjectsDeleterConfigurator> state, Reference ref) {
92
        if (state.getConfig().isKeepReferencesWithTitle() && isNotBlank(ref.getTitle())
93
                || state.getConfig().isKeepRisSources() && hasRISSource(ref)){
94
            return true;
95
        }else{
96
            return false;
97
        }
98
    }
99

    
100
    private String getSources(Reference ref) {
101
        String result = "";
102
        for (IdentifiableSource source : ref.getSources()){
103
            result += source.getType() + ": " + (source.getCitation() == null? "" : source.getCitation().getTitleCache()) + "\t";
104
        }
105
        return result;
106
    }
107

    
108
    private boolean hasRISSource(Reference ref) {
109
        for (IdentifiableSource source : ref.getSources()){
110
            Reference citation = source.getCitation();
111
            if (citation != null && citation.getTitleCache().startsWith("RIS Reference")){
112
                return true;
113
            }
114
        }
115
        return false;
116
    }
117

    
118
    @Override
119
	protected boolean doCheck(DefaultImportState<NonReferencedObjectsDeleterConfigurator> state) {
120
		return true;
121
	}
122

    
123
	@Override
124
	protected boolean isIgnore(DefaultImportState<NonReferencedObjectsDeleterConfigurator> state) {
125
		return false;
126
	}
127
}
(1-1/2)