Project

General

Profile

« Previous | Next » 

Revision da0260d4

Added by Andreas Müller over 6 years ago

ref #7075 activator for Greece image import and cleanup old Greek images

View differences:

app-import/src/main/java/eu/etaxonomy/cdm/app/common/CdmDestinations.java
311 311
    public static ICdmDataSource cdm_greece_checklist_production(){
312 312
        DatabaseTypeEnum dbType = DatabaseTypeEnum.MySQL;
313 313
        String cdmServer = "160.45.63.171";
314
        String cdmDB = "cdm_production_flora_hellenica";
314
        String cdmDB = "cdm_production_flora_greece";
315 315
        String cdmUserName = "edit";
316 316
        return makeDestination(dbType, cdmServer, cdmDB, -1, cdmUserName, null);
317 317
    }
app-import/src/main/java/eu/etaxonomy/cdm/app/cyprus/CyprusImagesActivator.java
331 331
            if (el.isInstanceOf(TextData.class)){
332 332
                textData = CdmBase.deproxy(el, TextData.class);
333 333
            }else{
334
                logger.warn("Image gallery had non-textdata description elmenet: " +  fileStr);
334
                logger.warn("Image gallery had non-textdata description element: " +  fileStr);
335 335
            }
336 336
        }
337 337
        if (textData == null){
app-import/src/main/java/eu/etaxonomy/cdm/app/greece/GreeceImageActivator.java
1
/**
2
* Copyright (C) 2016 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.app.greece;
10

  
11
import java.net.URI;
12
import java.util.UUID;
13

  
14
import org.apache.log4j.Logger;
15

  
16
import eu.etaxonomy.cdm.app.common.CdmDestinations;
17
import eu.etaxonomy.cdm.database.DbSchemaValidation;
18
import eu.etaxonomy.cdm.database.ICdmDataSource;
19
import eu.etaxonomy.cdm.io.common.CdmDefaultImport;
20
import eu.etaxonomy.cdm.io.common.IImportConfigurator.CHECK;
21
import eu.etaxonomy.cdm.io.common.ImportResult;
22
import eu.etaxonomy.cdm.io.media.in.MediaExcelImportConfigurator;
23
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
24
import eu.etaxonomy.cdm.model.reference.Reference;
25
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
26

  
27
/**
28
 *
29
 * Import for Checklist of Greece.
30
 *
31
 * https://dev.e-taxonomy.eu/redmine/issues/6286
32
 *
33
 * @author a.mueller
34
 * @date 13.12.2016
35
 */
36
public class GreeceImageActivator {
37
    @SuppressWarnings("unused")
38
    private static final Logger logger = Logger.getLogger(GreeceImageActivator.class);
39

  
40

  
41
//    static final ICdmDataSource cdmDestination = CdmDestinations.localH2();
42
//  static final ICdmDataSource cdmDestination = CdmDestinations.cdm_test_local_mysql_test();
43
    static final ICdmDataSource cdmDestination = CdmDestinations.cdm_greece_checklist_production();
44

  
45
//    private static final UUID sourceUuid = UUID.fromString("418b5885-08fb-4f1e-ac94-8f5c84b1683d");
46
    private static final UUID sourceUuid = UUID.fromString("c3d300f0-86ef-4c65-8727-c594035ed7a7");
47
//    private static final String fileName = "20171107_sent_1332_images.xlsx";
48
    private static final String fileName = "20171110_Turland_433_others_59.xlsx";
49

  
50

  
51
    //check - import
52
    static final CHECK check = CHECK.IMPORT_WITHOUT_CHECK;
53

  
54
    boolean doImages = true;
55

  
56
    private void doImport(ICdmDataSource cdmDestination){
57

  
58
        URI source = greekChecklist();  //just any
59
        //make Source
60
        MediaExcelImportConfigurator config = MediaExcelImportConfigurator.NewInstance(source, cdmDestination);
61
        config.setCheck(check);
62
        config.setDbSchemaValidation(DbSchemaValidation.VALIDATE);
63
        config.setSourceReference(getSourceReference());
64
        config.setNomenclaturalCode(NomenclaturalCode.ICNAFP);
65

  
66
        CdmDefaultImport<MediaExcelImportConfigurator> myImport = new CdmDefaultImport<>();
67
        ImportResult result = myImport.invoke(config);
68
        System.out.println(result.createReport());
69

  
70
    }
71

  
72

  
73
    private URI greekChecklist(){
74
        return URI.create("file:////BGBM-PESIHPC/Greece/images/" + fileName);
75
    }
76

  
77

  
78
    private Reference getSourceReference(){
79
        Reference result = ReferenceFactory.newDatabase();
80
        result.setTitle(fileName);
81
        result.setUuid(sourceUuid);
82

  
83
        return result;
84
    }
85

  
86
    /**
87
     * @param args
88
     */
89
    public static void main(String[] args) {
90
        GreeceImageActivator me = new GreeceImageActivator();
91
        me.doImport(cdmDestination);
92
        System.exit(0);
93
    }
94
}
app-import/src/main/java/eu/etaxonomy/cdm/app/greece/GreeceLargeImagesAdderActivator.java
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

  
10
package eu.etaxonomy.cdm.app.greece;
11

  
12
import java.net.URI;
13
import java.util.HashSet;
14
import java.util.List;
15
import java.util.Set;
16

  
17
import org.apache.log4j.Logger;
18
import org.springframework.transaction.TransactionStatus;
19

  
20
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
21
import eu.etaxonomy.cdm.app.common.CdmDestinations;
22
import eu.etaxonomy.cdm.common.media.ImageInfo;
23
import eu.etaxonomy.cdm.database.DbSchemaValidation;
24
import eu.etaxonomy.cdm.database.ICdmDataSource;
25
import eu.etaxonomy.cdm.io.api.application.CdmIoApplicationController;
26
import eu.etaxonomy.cdm.model.common.CdmBase;
27
import eu.etaxonomy.cdm.model.media.ImageFile;
28
import eu.etaxonomy.cdm.model.media.Media;
29
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
30
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
31

  
32
/**
33
 * @author a.mueller
34
 * @created 05.2017
35
 */
36
public class GreeceLargeImagesAdderActivator {
37
	@SuppressWarnings("unused")
38
    private static final Logger logger = Logger.getLogger(GreeceLargeImagesAdderActivator.class);
39

  
40

  
41
//	static final ICdmDataSource cdmDestination = CdmDestinations.localH2();
42
	static final ICdmDataSource cdmDestination = CdmDestinations.cdm_greece_checklist_production();
43

  
44
	static boolean testOnly = false;
45

  
46

  
47
    private void addLargeImage(ICdmDataSource cdmDestination){
48
        CdmApplicationController app = CdmIoApplicationController.NewInstance(cdmDestination, DbSchemaValidation.VALIDATE);
49
        TransactionStatus tx = app.startTransaction();
50

  
51
        List<Media> list = app.getMediaService().list(Media.class, null, null, null, null);
52
        for (Media media : list){
53
            if (! isFirstImportMedia(media)){
54
                continue;
55
            }
56
            Set<MediaRepresentation> reps = media.getRepresentations();
57
            if (reps.size() != 2){
58
                System.out.println("Media has not exactly 2 representations: " +  media.getId() + "; " +  media.getTitleCache());
59
                continue;
60
            }else{
61
                MediaRepresentation first = reps.iterator().next();
62
                if (first.getParts().size() != 1){
63
                    System.out.println("Media has representation with not exactly 1 parts: " +  media.getId() + "; " +  media.getTitleCache());
64
                    continue;
65
                }
66
                MediaRepresentationPart part = first.getParts().iterator().next();
67
                String uri = part.getUri().toString();
68
                if (uri.startsWith("https://media.e-taxonomy.eu/flora-greece/medium/Plate")){
69
                    uri = uri.replace("flora-greece/medium/Plate", "flora-greece/large/Plate");
70
                }else if(uri.startsWith("https://media.e-taxonomy.eu/flora-greece/thumbs/Plate")) {
71
                    uri = uri.replace("flora-greece/thumbs/Plate", "flora-greece/large/Plate");
72
                }else{
73
                    System.out.println("URI has unexpected format: " +  uri);
74
                    continue;
75
                }
76
                handleUri(media, uri);
77
            }
78
        }
79
        if (testOnly){
80
            tx.setRollbackOnly();
81
        }
82
        app.commitTransaction(tx);
83
    }
84

  
85
	private void updateImageSizes(ICdmDataSource cdmDestination){
86
        CdmApplicationController app = CdmIoApplicationController.NewInstance(cdmDestination, DbSchemaValidation.VALIDATE);
87
        TransactionStatus tx = app.startTransaction();
88

  
89
        List<Media> list = app.getMediaService().list(Media.class, null, null, null, null);
90
        for (Media media : list){
91
            if (! isFirstImportMedia(media)){
92
                continue;
93
            }
94
            Set<MediaRepresentation> reps = media.getRepresentations();
95
            for (MediaRepresentation rep : reps){
96
                for (MediaRepresentationPart part : rep.getParts()){
97
                    if (part.isInstanceOf(ImageFile.class)){
98
                        handlePart(CdmBase.deproxy(part, ImageFile.class));
99
                    }else{
100
                        System.out.println("Representation part is not of type ImageFile: "+  part.getId());
101
                    }
102
                }
103
            }
104
        }
105

  
106
        if (testOnly){
107
            tx.setRollbackOnly();
108
        }
109
        app.commitTransaction(tx);
110

  
111
	}
112

  
113

  
114

  
115
    /**
116
     * @param media
117
     * @return
118
     */
119
    private boolean isFirstImportMedia(Media media) {
120
        Boolean result = null;
121
        Set<String> urls = getUrlStringForMedia(media);
122
        for (String url : urls){
123
            if (url.startsWith("http://150.140.202.8/files/Goula/") || url.startsWith("http://n4412.gr/images/Globula")){
124
                if (result == Boolean.TRUE){
125
                    System.out.println("Ambigous: "  + media.getId());
126
                    return false;
127
                }
128
                result = false;
129
            }else if (url.startsWith("https://media.e-taxonomy.eu/flora-greece")){
130
                if (result == Boolean.FALSE){
131
                    System.out.println("Ambigous: "  + media.getId());
132
                    return false;
133
                }
134
                result = true;
135
            }
136
        }
137
        if (result == null){
138
            System.out.println("No data: "  + media.getId());
139
            return false;
140
        }
141
        return result;
142
    }
143

  
144
    /**
145
     * @param media
146
     * @return
147
     */
148
    private Set<String> getUrlStringForMedia(Media media) {
149
        Set<String> result = new HashSet<>();
150
        for (MediaRepresentation rep : media.getRepresentations()){
151
            for (MediaRepresentationPart part : rep.getParts()){
152
                URI uri = part.getUri();
153
                if (uri != null){
154
                    result.add(uri.toString());
155
                }else{
156
                    System.out.println("URI is null:" + media.getId());
157
                }
158
            }
159
        }
160
        return result;
161
    }
162

  
163
    /**
164
     * @param part
165
     */
166
    private void handlePart(ImageFile part) {
167
        ImageInfo imageInfo = null;
168
        URI uri = part.getUri();
169
        try {
170
            imageInfo = ImageInfo.NewInstance(uri, 0);
171
        } catch (Exception e) {
172
            String message = "An error occurred when trying to read image meta data for %s.";
173
            message = String.format(message, uri.toString());
174
            System.out.println(message);
175
            return;
176
        }
177
        part.setHeight(imageInfo.getHeight());
178
        part.setWidth(imageInfo.getWidth());
179

  
180
        MediaRepresentation representation = part.getMediaRepresentation();
181
        representation.setMimeType(imageInfo.getMimeType());
182
        representation.setSuffix(imageInfo.getSuffix());
183

  
184
    }
185

  
186
    /**
187
     * @param reps
188
     * @param uri
189
     */
190
    private void handleUri(Media media, String uriStr) {
191
        URI uri = URI.create(uriStr);
192
        ImageInfo imageInfo = null;
193
        try {
194
            imageInfo = ImageInfo.NewInstance(uri, 0);
195
        } catch (Exception e) {
196
            String message = "An error occurred when trying to read image meta data for %s.";
197
            message = String.format(message, uri.toString());
198
            System.out.println(message);
199
        }
200
        ImageFile imageFile = ImageFile.NewInstance(uri, null, imageInfo);
201

  
202
        MediaRepresentation representation = MediaRepresentation.NewInstance();
203

  
204
        if(imageInfo != null){
205
            representation.setMimeType(imageInfo.getMimeType());
206
            representation.setSuffix(imageInfo.getSuffix());
207
        }
208
        representation.addRepresentationPart(imageFile);
209
        media.addRepresentation(representation);
210

  
211
    }
212

  
213

  
214

  
215

  
216
    /**
217
	 * @param args
218
	 */
219
	public static void main(String[] args) {
220
		GreeceLargeImagesAdderActivator me = new GreeceLargeImagesAdderActivator();
221
//		me.addLargeImage(cdmDestination);
222
		me.updateImageSizes(cdmDestination);
223
//		me.test();
224
		System.exit(0);
225
	}
226

  
227
}

Also available in: Unified diff