Project

General

Profile

Download (4.73 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.io.operation.config.DeleteNonReferencedReferencesConfigurator;
20
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
21
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
22
import eu.etaxonomy.cdm.model.reference.Reference;
23
import eu.etaxonomy.cdm.persistence.query.OrderHint;
24

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

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

    
34
    @Override
35
	protected void doInvoke(DefaultImportState<DeleteNonReferencedReferencesConfigurator> state) {
36

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

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

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

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

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

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

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

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

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

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