Project

General

Profile

Download (7 KB) Statistics
| Branch: | Revision:
1
/**
2
* Copyright (C) 2017 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.greece;
10

    
11
import java.io.File;
12
import java.io.IOException;
13
import eu.etaxonomy.cdm.common.URI;
14
import java.util.List;
15

    
16
import org.apache.commons.imaging.ImageReadException;
17
import org.apache.commons.imaging.Imaging;
18
import org.apache.commons.imaging.common.GenericImageMetadata.GenericImageMetadataItem;
19
import org.apache.commons.imaging.common.ImageMetadata;
20
import org.apache.commons.imaging.common.ImageMetadata.ImageMetadataItem;
21
import org.apache.http.HttpException;
22
import org.apache.log4j.Logger;
23
import org.joda.time.DateTime;
24
import org.joda.time.DateTimeZone;
25
import org.joda.time.format.DateTimeFormat;
26
import org.joda.time.format.DateTimeFormatter;
27

    
28
import eu.etaxonomy.cdm.common.media.CdmImageInfo;
29
import eu.etaxonomy.cdm.io.common.utils.ImportDeduplicationHelper;
30
import eu.etaxonomy.cdm.model.agent.AgentBase;
31
import eu.etaxonomy.cdm.model.common.TimePeriod;
32
import eu.etaxonomy.cdm.model.description.TaxonDescription;
33
import eu.etaxonomy.cdm.model.description.TextData;
34
import eu.etaxonomy.cdm.model.media.ImageFile;
35
import eu.etaxonomy.cdm.model.media.Media;
36
import eu.etaxonomy.cdm.model.taxon.Taxon;
37

    
38
/**
39
 * @author a.mueller
40
 * @since 13.05.2017
41
 */
42
public class ImageImportTest {
43

    
44
    @SuppressWarnings("unused")
45
    private static final Logger logger = Logger.getLogger(FloraHellenicaImageImport.class);
46

    
47
    private static final String BASE_URL = "https://media.e-taxonomy.eu/flora-greece/";
48
    private static final String IMAGE_FOLDER = "////BGBM-PESIHPC/Greece/thumbs/";
49
    private ImportDeduplicationHelper deduplicationHelper = null;
50

    
51
    protected void doInvoke() {
52
        for (int plate = 1; plate < 22 ; plate++){
53
            System.out.println("Plate: " + plate);
54
            String fill = plate < 10 ? "0" : "";
55
            String plateStr = "Plate_" + fill + plate + "/";
56
            String fullFolderUrl = BASE_URL + plateStr;
57
            String fullThumbUrl = BASE_URL + "thumbs/" + plateStr;
58
            String folderStr = IMAGE_FOLDER + plateStr;
59
            File file = new File(folderStr);
60
            String[] list = file.list();
61
            System.out.println(DateTimeZone.getAvailableIDs());
62
            for (String fileStr : list){
63
                String[] taxonNameAndArtist = getTaxonName(fileStr);
64
                String taxonNameStr = taxonNameAndArtist[0];
65
                String artistStr = taxonNameAndArtist[1];
66

    
67
                if(false){
68
                    continue;
69
                }
70
                Taxon taxon = getAcceptedTaxon(taxonNameStr);
71
                TaxonDescription imageGallery = taxon.getImageGallery(true);
72
                TextData textData = TextData.NewInstance();
73

    
74
                URI uri = URI.create(fullFolderUrl + fileStr);
75

    
76
                //image metadata
77
                File imageFile = new File("");
78
                ImageMetadata metadata;
79
                try {
80
                    metadata = Imaging.getMetadata(uri.toURL().openStream(), null);
81
                    List<? extends ImageMetadataItem> items = metadata.getItems();
82
                    for (ImageMetadataItem metadataItem : items){
83
                        if (metadataItem instanceof GenericImageMetadataItem){
84
                            GenericImageMetadataItem item = (GenericImageMetadataItem) metadataItem;
85
    //                      System.out.println(item.getKeyword() +  ":    " + item.getText());
86
                            String value = removeQuots(item.getText());
87
                            if("Image Description".equalsIgnoreCase(item.getKeyword())){
88
    //                          media.putDescription(Language.DEFAULT(), item.getText());
89
                            }else if ("date time original".equalsIgnoreCase(item.getKeyword())){
90
                                DateTimeFormatter f = DateTimeFormat.forPattern("yyyy:MM:dd HH:mm:ss");
91
                                DateTime created = f.withZone(DateTimeZone.forID("Europe/Athens")).parseDateTime(value);
92
                                System.out.println(created);
93
                            }
94
                        }else{
95
                            throw new IllegalStateException("Unsupported ImageMetadataItem type: " + metadataItem.getClass().getName());
96
                        }
97
                    }
98
                } catch (ImageReadException | IOException e1) {
99
                    // TODO Auto-generated catch block
100
                    e1.printStackTrace();
101
                }
102

    
103
                CdmImageInfo imageMetaData;
104
                try {
105
                    imageMetaData = CdmImageInfo.NewInstance(uri, 0);
106

    
107
                    String mimeType = imageMetaData.getMimeType();
108
                    String suffix = null;
109
                    int height = imageMetaData.getHeight();
110
                    int width = imageMetaData.getWidth();
111
                    Integer size = null;
112
                    TimePeriod mediaCreated = null;
113
                    AgentBase<?> artist = null;
114
                    Media media = ImageFile.NewMediaInstance(mediaCreated, artist, uri, mimeType, suffix, size, height, width);
115

    
116
                    textData.addMedia(media);
117
                    imageGallery.addElement(textData);
118
                } catch (IOException | HttpException e) {
119
                    // TODO Auto-generated catch block
120
                    e.printStackTrace();
121
                }
122
            }
123
        }
124
    }
125
    private String removeQuots(String text) {
126
        if (text.startsWith("'") && text.endsWith("'")){
127
            return text.substring(1, text.length() -1);
128
        }else{
129
            return text;
130
        }
131
    }
132

    
133
    private Taxon getAcceptedTaxon(String taxonNameStr) {
134
        return Taxon.NewInstance(null, null);
135
    }
136

    
137
    private String[] getTaxonName(String fileStr) {
138
        String[] result = new String[2];
139
        fileStr = fileStr.split("\\.")[0];
140
        fileStr = fileStr.replaceAll("[0-9]", "");
141
        String[] x = fileStr.split("_");
142
        if (x.length == 2){
143
            result[1] = x[1];
144
        }
145

    
146
        fileStr = splitCamelCase(x[0]);
147
        String[] split = fileStr.split(" ");
148
        String name = split[0] + " " + split[1].toLowerCase() +
149
                (split.length > 2 ? " subsp. " + split[2].toLowerCase() : "");
150
        result[0] = name;
151
        System.out.println(result[0] + (result[1] != null ?  "   Artist: " + result[1]: ""));
152
        return result;
153
    }
154

    
155
    //from http://stackoverflow.com/questions/2559759/how-do-i-convert-camelcase-into-human-readable-names-in-java
156
    static String splitCamelCase(String s) {
157
        return s.replaceAll(
158
           String.format("%s",
159
//              "(?<=[A-Z])(?=[A-Z][a-z])",
160
              "(?<=[^A-Z])(?=[A-Z])"
161
//              "(?<=[A-Za-z])(?=[^A-Za-z])"
162
           ),
163
           " "
164
        );
165
     }
166

    
167
    public static void main(String[] str){
168
        ImageImportTest test = new ImageImportTest();
169
        test.doInvoke();
170
    }
171
}
(18-18/18)