Project

General

Profile

Download (7.87 KB) Statistics
| Branch: | 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

    
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
 * @since 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
}
(6-6/7)