Project

General

Profile

1
package eu.dnetlib.data.claims.migration;
2

    
3
import eu.dnetlib.data.claims.migration.entity.*;
4
import eu.dnetlib.data.claimsDemo.ClaimUtils;
5
import org.apache.log4j.Logger;
6

    
7
import java.io.BufferedWriter;
8
import java.io.FileWriter;
9
import java.io.PrintWriter;
10

    
11
/**
12
 * Created by argirok on 21/1/2016.
13
 */
14
public class ClaimValidation {
15
    private PrintWriter out;
16
    String pathToSaveReport = null;
17

    
18
    private static Logger logger = Logger.getLogger(ClaimValidation.class);
19

    
20
    /*
21
    Claim:
22
    Mandatory Fields : id, sourceType,targetType, User e-mail, Date, Target, Source
23

    
24
    */
25
    public  boolean validateClaim(Claim claim){
26
        boolean isValid=true;
27
        try {
28
            out = new PrintWriter(new BufferedWriter(new FileWriter(pathToSaveReport+"/validationFile.txt", true)));
29

    
30
        } catch (Exception e) {
31
            out = null;
32
            logger.error("Couldn't write to file " + "validationFile.txt", e);
33
        }
34

    
35
              if(claim==null){
36
                isValid=false;
37
                if(out!=null) {
38
                    out.println("Claim invalid (null)...");
39
                }
40
                logger.warn("Claim invalid (null)...");
41
//            }else if(claim.getId()==null||claim.getId().isEmpty()){
42
//                isValid=false;
43
//                 out.println("Claim invalid  (id)..." );
44
             }else if(claim.getTargetType()==null||claim.getTargetType().isEmpty()){
45
                isValid=false;
46
                if(out!=null) {
47
                    out.println("Claim invalid (target type): " + claim.getId());
48
                }
49
                logger.warn("Claim invalid (target type): "+claim.getId());
50

    
51
            }else if(claim.getSourceType()==null||claim.getSourceType().isEmpty()){
52
                isValid=false;
53
                if(out!=null) {
54
                    out.println("Claim invalid (source type): " + claim.getId());
55
                }
56
                logger.warn("Claim invalid (source type): "+claim.getId());
57

    
58
            }else if(claim.getDate()==null){
59
                isValid=false;
60
                if(out!=null) {
61
                    out.println("Claim invalid (date): " + claim.getId());
62
                }
63
                logger.warn("Claim invalid (date): "+claim.getId());
64

    
65
            }else if(claim.getUserMail()==null||claim.getUserMail().isEmpty()){
66
                isValid=false;
67
                 if(out!=null) {
68
                     out.println("Claim invalid (mail): " + claim.getId());
69
                 }
70
                logger.warn("Claim invalid (mail): "+claim.getId());
71

    
72
            }else if(claim.getSource()==null || claim.getTarget()==null){
73
                isValid=false;
74
                 if(out!=null) {
75
                     out.println("Result invalid : " + claim.getId());
76
                 }
77
                logger.warn("Result invalid : "+claim.getId());
78

    
79
            }else if(claim.getSource()==null || claim.getTarget()==null){
80
                isValid=false;
81
                 if(out!=null) {
82
                     out.println("Result invalid : " + claim.getId());
83
                 }
84
                logger.warn("Result invalid : "+claim.getId());
85

    
86
            }else if(claim.getSource().getOpenaireId()!=null && claim.getTarget().getOpenaireId()!=null && claim.getSource().getOpenaireId().equals(claim.getTarget().getOpenaireId())){
87
                isValid=false;
88
                 if(out!=null) {
89
                     out.println("Claim invalid (source == target): " + claim.getId());
90
                 }
91
                logger.warn("Claim invalid (source == target): "+claim.getId());
92

    
93
            }else{
94
                boolean sourceIsValid=validateByType(claim.getSource(),claim.getSourceType(),claim.getId());
95
                boolean targetIsValid=validateByType(claim.getTarget(),claim.getTargetType(),claim.getId());
96
                isValid= isValid&&sourceIsValid&&targetIsValid;
97
                 if(!isValid){
98
                     if(out!=null) {
99
                         out.println("Claim invalid (source /target): " + claim.getId() + "\n");
100
                     }
101
                     logger.warn("Claim invalid (source /target): "+claim.getId()+"\n");
102
                 }
103
            }
104
            if(out!=null) {
105
                out.close();
106
            }
107

    
108
        return isValid;
109
    }
110
    /*
111
    Project:
112
    Mandatory Fields : id, type, Collected from
113

    
114
     */
115
    public  boolean validateResult(Result result, String claimId){
116
        boolean isValid=true;
117
            if(result==null){
118
                isValid=false;
119
                out.println("Result invalid (null): - claimId:"+claimId);
120
//            }else if(result.getOpenaireId()==null||result.getOpenaireId().isEmpty()){
121
//                isValid=false;
122
//                out.println("Result invalid (Id): - claimId:"+claimId);
123
            }else if(result.getResultType()==null||result.getResultType().isEmpty()){
124
                isValid=false;
125
                out.println("Result invalid (Type): "+result.getOpenaireId()+"- claimId:"+claimId);
126
            }/*
127
            //http://services.openaire.eu:8380/search/search?action=search&sTransformer=results_openaire&query=%28%28%28deletedbyinference+%3D+false%29+AND+%28oaftype+exact+result%29%29+and+%28%28resulttypeid+exact+publication%29+or+%28resulttypeid+exact+dataset%29%29%29+and+%28%28objIdentifier+
128
            //exact+od______1389%3A%3A6ba133c8460d5a67dfb18f34cc5303fb%29+or+%28resultdupid+exact+od______1389%3A%3A6ba133c8460d5a67dfb18f34cc5303fb%29%29&size=10&locale=en_GB
129
            //There are resuts with no title
130
            //so I am
131
            else if(result.getTitle()==null||result.getTitle().isEmpty()){
132
                isValid=false;
133
                out.println("Result invalid (title): "+result.getOpenaireId()+"- claimId:"+claimId);
134
            }*/else if(result.getCollectedFrom()==null||result.getCollectedFrom().isEmpty()){
135
                isValid=false;
136
                out.println("Result invalid (collectedFrom) : "+result.getOpenaireId()+"- claimId:"+claimId);
137
            }else if (result.getCollectedFrom().equals(ClaimUtils.COLLECTED_FROM_OPENAIRE) && result.getOpenaireId()==null) {
138
                isValid = false;
139
                out.println("Result invalid (Openaire - no id): - claimId:" + claimId);
140
            }else if ((result.getCollectedFrom().equals(ClaimUtils.COLLECTED_FROM_DATACITE)|| result.getCollectedFrom().equals(ClaimUtils.COLLECTED_FROM_CROSSREF) ) && result.getDoi()==null) {
141
                isValid = false;
142
                out.println("Result invalid (Crossref/Datacite - no doi): - claimId:" + claimId);
143
            }else if (result.getCollectedFrom().equals(ClaimUtils.COLLECTED_FROM_ORCID) && result.getOrcidworkid()==null) {
144
                isValid = false;
145
                out.println("Result invalid (Orcid - no work id): - claimId:" + claimId);
146
            }
147

    
148
        return isValid;
149
    }
150
    /*
151
    Project:
152
    Mandatory Fields : id, name
153

    
154
     */
155
    public  boolean validateProject(Project project, String claimId){
156
        boolean isValid=true;
157
             if(project==null){
158
                isValid=false;
159
                out.println("Project invalid (null): - claimId:"+claimId);
160
            }else if(project.getOpenaireId()==null||project.getOpenaireId().isEmpty()){
161
                isValid=false;
162
                out.println("Project invalid (id): - claimId:"+claimId);
163
            }else if((project.getName()==null||project.getName().isEmpty())&&(project.getAcronym()==null||project.getAcronym().isEmpty())){
164
                isValid=false;
165
                out.println("Project invalid (name/acronym): "+project.getOpenaireId()+"- claimId:"+claimId);
166
            }else if(project.getFunderId()==null||project.getFunderId().isEmpty()){
167
                isValid=false;
168
                out.println("Project invalid (funder id): "+project.getOpenaireId()+"- claimId:"+claimId);
169
            }
170
            else if(project.getFunderName()==null||project.getFunderName().isEmpty()){
171
                isValid=false;
172
                out.println("Project invalid (funder name): "+project.getOpenaireId()+"- claimId:"+claimId);
173
            }
174

    
175
        return isValid;
176
    }
177
    /*
178
    Context:
179
    Mandatory Fields : id, title
180

    
181
     */
182
    public  boolean validateContext(Context context, String claimId){
183
        boolean isValid=true;
184
            if(context==null){
185
                isValid=false;
186
                out.println("Context invalid (null): - claimId:"+claimId);
187
            }else if(context.getOpenaireId()==null||context.getOpenaireId().isEmpty()){
188
                isValid=false;
189
                out.println("Context invalid (id): - claimId:"+claimId);
190
            }/*
191
            //This is commented since There isn't a way to fill this field right now.
192
            else if(context.getTitle()==null||context.getTitle().isEmpty()){
193
                isValid=false;
194
                out.println("Context invalid : "+context.getOpenaireId()+"- claimId:"+claimId);
195
            }*/
196
        return isValid;
197
    }
198
    private boolean validateByType(OpenaireEntity entity,String type , String claimId ){
199
        //TODO add more types (e.g. patends) when they are available
200
        if(type.equals(ClaimUtils.PUBLICATION)||type.equals(ClaimUtils.DATASET)||type.equals(ClaimUtils.SOFTWARE) ||type.equals(ClaimUtils.OTHER)){
201
            return validateResult((Result)entity,claimId);
202
        } else if(type.equals(ClaimUtils.CONTEXT)){
203
            return validateContext((Context)entity,claimId);
204
        }else if(type.equals(ClaimUtils.PROJECT)){
205
            return validateProject((Project)entity,claimId);
206
        }else{
207
            if(entity.getOpenaireId()!=null){
208
                out.println("Entity invalid type ("+type+"):"+entity.getOpenaireId()+" - claimId:"+claimId);
209
            }else {
210
                out.println("Entity invalid type (" + type + "): - claimId:" + claimId);
211
            }
212
        }
213
        return false;
214
    }
215

    
216
    public static boolean validateCollectedFrom(String collectedFrom) throws ClaimValidationException {
217
        //TODO add more collected from when they are available
218
        if (collectedFrom==null || collectedFrom.isEmpty()) {
219
            logger.error("CollectedFrom field of source and/or target is null or empty");
220
            throw new ClaimValidationException("CollectedFrom field of source and/or target cannot be empty.");
221

    
222
        } else if (!collectedFrom.equals(ClaimUtils.OPENAIRE) &&
223
                !collectedFrom.equals(ClaimUtils.DATACITE) &&
224
                !collectedFrom.equals(ClaimUtils.CROSSREF) &&
225
                !collectedFrom.equals(ClaimUtils.ORCID)) {
226
            logger.error("CollectedFrom field of source and/or target is invalid. Value is:"+collectedFrom);
227
            throw new ClaimValidationException("CollectedFrom field of source and/or target is invalid. The allowed " +
228
                    "types are: " + ClaimUtils.OPENAIRE + ", " + ClaimUtils.DATACITE
229
                    + ", " + ClaimUtils.CROSSREF + ", " + ClaimUtils.ORCID +".");
230

    
231

    
232
        }
233

    
234
        return true;
235
    }
236

    
237
    public static boolean validateType(String type)  throws ClaimValidationException {
238
        if (type == null || type.isEmpty()) {
239
            throw new ClaimValidationException("The type of source and/or target cannot be empty.");
240

    
241
        } else if (!type.equals(ClaimUtils.PUBLICATION) && !type.equals(ClaimUtils.DATASET) &&
242
                !type.equals(ClaimUtils.CONTEXT) && !type.equals(ClaimUtils.PROJECT)) {
243
            throw new ClaimValidationException("The given type is invalid. The allowed types are: " +
244
                    ClaimUtils.PUBLICATION + ", " + ClaimUtils.DATASET + ", " + ClaimUtils.CONTEXT + ", " +
245
                    ClaimUtils.PROJECT);
246
        }
247

    
248
        return true;
249
    }
250

    
251
//    /*
252
//   Checks if the doi is valid. If it is not  then replaces the existing  DOI with the valid one.
253
//    */
254
//    public static void checkForDOI(Result r) throws Exception {
255
//        if(r.getDOI()==null){
256
//            return ;
257
//        }
258
//         String validDOI=isValidDoi(r.getDOI());
259
//        if(!validDOI.equals(r.getDOI())){
260
//            if(r.getMetadataRecord()!=null) {
261
//                r.setMetadataRecord(r.getMetadataRecord().replace(r.getDOI(), validDOI));
262
//            }
263
//            if(r.getExternalUrl()!=null){
264
//                r.setExternalUrl(r.getExternalUrl().replace(r.getDOI(), validDOI));
265
//            }
266
//            r.setDoi(validDOI);
267
//        }
268
//    }
269
//    public static String isValidDoi(String doi) throws Exception {
270
//        if(doi==null){
271
//            return null;
272
//        }
273
//        try{
274
//            PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("invalid_dois.txt", true)));
275
//            boolean valid=SearchUtils.isDoiValid(doi);
276
//            if(!valid&&(doi.length() - (doi.replace("_", "")).length() > 1)){
277
//                String transformDoi = doi.replace("_", "-");
278
//                valid=SearchUtils.isDoiValid(transformDoi);
279
//                if(valid){
280
//                    Date date= new java.util.Date();
281
//                    out.println(new Timestamp(date.getTime())+" - Invalid doi: "+doi+" replaced with : "+transformDoi);
282
//                    doi=transformDoi;
283
//                }
284
//            }else if(!valid&&(doi.length() - (doi.replace("_", "")).length() == 1)) {
285
//                String transformDoi = doi.replace("_", "-");
286
//                valid=SearchUtils.isDoiValid(transformDoi);
287
//                if(valid){
288
//                    Date date= new java.util.Date();
289
//                    out.println(new Timestamp(date.getTime())+" - Invalid doi: "+doi+" replaced with : "+transformDoi);
290
//                    doi = transformDoi;
291
//                }
292
//            }else if(!valid) {
293
//                String transformDoi =doi.replaceAll("\\p{C}", "");
294
//                valid=SearchUtils.isDoiValid(transformDoi);
295
//                if(valid){
296
//                    Date date= new java.util.Date();
297
//                    out.println(new Timestamp(date.getTime())+" - Invalid doi: "+doi+" replaced with (zero-width-char): "+transformDoi);
298
//                    doi = transformDoi;
299
//                }
300
//            }
301
//            out.close();
302
//        }catch (IOException e) {
303
//            e.printStackTrace();
304
//            System.err.println("Couldn't write to file " + "invalid_dois.txt");
305
//        }
306
//        return doi;
307
//    }
308

    
309

    
310
    public String getPathToSaveReport() {
311
        return pathToSaveReport;
312
    }
313

    
314
    public void setPathToSaveReport(String pathToSaveReport) {
315
        this.pathToSaveReport = pathToSaveReport;
316
    }
317
}
(2-2/6)