ref #7075 activator for Greece image import and cleanup old Greek images
authorAndreas Müller <a.mueller@bgbm.org>
Sat, 11 Nov 2017 21:24:40 +0000 (22:24 +0100)
committerAndreas Müller <a.mueller@bgbm.org>
Sat, 11 Nov 2017 21:24:40 +0000 (22:24 +0100)
app-import/src/main/java/eu/etaxonomy/cdm/app/common/CdmDestinations.java
app-import/src/main/java/eu/etaxonomy/cdm/app/cyprus/CyprusImagesActivator.java
app-import/src/main/java/eu/etaxonomy/cdm/app/greece/GreeceImageActivator.java [new file with mode: 0644]
app-import/src/main/java/eu/etaxonomy/cdm/app/greece/GreeceLargeImagesAdderActivator.java [new file with mode: 0644]

index 680a9aee98d8c0d1cadb66f0cc804a4e741c0652..e6af53cbbf167008ac2b7d74d22dd3a41308ddfa 100644 (file)
@@ -311,7 +311,7 @@ public class CdmDestinations {
     public static ICdmDataSource cdm_greece_checklist_production(){
         DatabaseTypeEnum dbType = DatabaseTypeEnum.MySQL;
         String cdmServer = "160.45.63.171";
-        String cdmDB = "cdm_production_flora_hellenica";
+        String cdmDB = "cdm_production_flora_greece";
         String cdmUserName = "edit";
         return makeDestination(dbType, cdmServer, cdmDB, -1, cdmUserName, null);
     }
index f7288a85a948fd1237a4efd05dcb5fb6020eac32..68e799e5ede9beff66641d1864bb31fad2d9a076 100644 (file)
@@ -331,7 +331,7 @@ public class CyprusImagesActivator {
             if (el.isInstanceOf(TextData.class)){
                 textData = CdmBase.deproxy(el, TextData.class);
             }else{
-                logger.warn("Image gallery had non-textdata description elmenet: " +  fileStr);
+                logger.warn("Image gallery had non-textdata description element: " +  fileStr);
             }
         }
         if (textData == null){
diff --git a/app-import/src/main/java/eu/etaxonomy/cdm/app/greece/GreeceImageActivator.java b/app-import/src/main/java/eu/etaxonomy/cdm/app/greece/GreeceImageActivator.java
new file mode 100644 (file)
index 0000000..2e32e16
--- /dev/null
@@ -0,0 +1,94 @@
+/**
+* Copyright (C) 2016 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.cdm.app.greece;
+
+import java.net.URI;
+import java.util.UUID;
+
+import org.apache.log4j.Logger;
+
+import eu.etaxonomy.cdm.app.common.CdmDestinations;
+import eu.etaxonomy.cdm.database.DbSchemaValidation;
+import eu.etaxonomy.cdm.database.ICdmDataSource;
+import eu.etaxonomy.cdm.io.common.CdmDefaultImport;
+import eu.etaxonomy.cdm.io.common.IImportConfigurator.CHECK;
+import eu.etaxonomy.cdm.io.common.ImportResult;
+import eu.etaxonomy.cdm.io.media.in.MediaExcelImportConfigurator;
+import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
+import eu.etaxonomy.cdm.model.reference.Reference;
+import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
+
+/**
+ *
+ * Import for Checklist of Greece.
+ *
+ * https://dev.e-taxonomy.eu/redmine/issues/6286
+ *
+ * @author a.mueller
+ * @date 13.12.2016
+ */
+public class GreeceImageActivator {
+    @SuppressWarnings("unused")
+    private static final Logger logger = Logger.getLogger(GreeceImageActivator.class);
+
+
+//    static final ICdmDataSource cdmDestination = CdmDestinations.localH2();
+//  static final ICdmDataSource cdmDestination = CdmDestinations.cdm_test_local_mysql_test();
+    static final ICdmDataSource cdmDestination = CdmDestinations.cdm_greece_checklist_production();
+
+//    private static final UUID sourceUuid = UUID.fromString("418b5885-08fb-4f1e-ac94-8f5c84b1683d");
+    private static final UUID sourceUuid = UUID.fromString("c3d300f0-86ef-4c65-8727-c594035ed7a7");
+//    private static final String fileName = "20171107_sent_1332_images.xlsx";
+    private static final String fileName = "20171110_Turland_433_others_59.xlsx";
+
+
+    //check - import
+    static final CHECK check = CHECK.IMPORT_WITHOUT_CHECK;
+
+    boolean doImages = true;
+
+    private void doImport(ICdmDataSource cdmDestination){
+
+        URI source = greekChecklist();  //just any
+        //make Source
+        MediaExcelImportConfigurator config = MediaExcelImportConfigurator.NewInstance(source, cdmDestination);
+        config.setCheck(check);
+        config.setDbSchemaValidation(DbSchemaValidation.VALIDATE);
+        config.setSourceReference(getSourceReference());
+        config.setNomenclaturalCode(NomenclaturalCode.ICNAFP);
+
+        CdmDefaultImport<MediaExcelImportConfigurator> myImport = new CdmDefaultImport<>();
+        ImportResult result = myImport.invoke(config);
+        System.out.println(result.createReport());
+
+    }
+
+
+    private URI greekChecklist(){
+        return URI.create("file:////BGBM-PESIHPC/Greece/images/" + fileName);
+    }
+
+
+    private Reference getSourceReference(){
+        Reference result = ReferenceFactory.newDatabase();
+        result.setTitle(fileName);
+        result.setUuid(sourceUuid);
+
+        return result;
+    }
+
+    /**
+     * @param args
+     */
+    public static void main(String[] args) {
+        GreeceImageActivator me = new GreeceImageActivator();
+        me.doImport(cdmDestination);
+        System.exit(0);
+    }
+}
diff --git a/app-import/src/main/java/eu/etaxonomy/cdm/app/greece/GreeceLargeImagesAdderActivator.java b/app-import/src/main/java/eu/etaxonomy/cdm/app/greece/GreeceLargeImagesAdderActivator.java
new file mode 100644 (file)
index 0000000..b83d50e
--- /dev/null
@@ -0,0 +1,227 @@
+/**
+* Copyright (C) 2007 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+
+package eu.etaxonomy.cdm.app.greece;
+
+import java.net.URI;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.log4j.Logger;
+import org.springframework.transaction.TransactionStatus;
+
+import eu.etaxonomy.cdm.api.application.CdmApplicationController;
+import eu.etaxonomy.cdm.app.common.CdmDestinations;
+import eu.etaxonomy.cdm.common.media.ImageInfo;
+import eu.etaxonomy.cdm.database.DbSchemaValidation;
+import eu.etaxonomy.cdm.database.ICdmDataSource;
+import eu.etaxonomy.cdm.io.api.application.CdmIoApplicationController;
+import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.model.media.ImageFile;
+import eu.etaxonomy.cdm.model.media.Media;
+import eu.etaxonomy.cdm.model.media.MediaRepresentation;
+import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
+
+/**
+ * @author a.mueller
+ * @created 05.2017
+ */
+public class GreeceLargeImagesAdderActivator {
+       @SuppressWarnings("unused")
+    private static final Logger logger = Logger.getLogger(GreeceLargeImagesAdderActivator.class);
+
+
+//     static final ICdmDataSource cdmDestination = CdmDestinations.localH2();
+       static final ICdmDataSource cdmDestination = CdmDestinations.cdm_greece_checklist_production();
+
+       static boolean testOnly = false;
+
+
+    private void addLargeImage(ICdmDataSource cdmDestination){
+        CdmApplicationController app = CdmIoApplicationController.NewInstance(cdmDestination, DbSchemaValidation.VALIDATE);
+        TransactionStatus tx = app.startTransaction();
+
+        List<Media> list = app.getMediaService().list(Media.class, null, null, null, null);
+        for (Media media : list){
+            if (! isFirstImportMedia(media)){
+                continue;
+            }
+            Set<MediaRepresentation> reps = media.getRepresentations();
+            if (reps.size() != 2){
+                System.out.println("Media has not exactly 2 representations: " +  media.getId() + "; " +  media.getTitleCache());
+                continue;
+            }else{
+                MediaRepresentation first = reps.iterator().next();
+                if (first.getParts().size() != 1){
+                    System.out.println("Media has representation with not exactly 1 parts: " +  media.getId() + "; " +  media.getTitleCache());
+                    continue;
+                }
+                MediaRepresentationPart part = first.getParts().iterator().next();
+                String uri = part.getUri().toString();
+                if (uri.startsWith("https://media.e-taxonomy.eu/flora-greece/medium/Plate")){
+                    uri = uri.replace("flora-greece/medium/Plate", "flora-greece/large/Plate");
+                }else if(uri.startsWith("https://media.e-taxonomy.eu/flora-greece/thumbs/Plate")) {
+                    uri = uri.replace("flora-greece/thumbs/Plate", "flora-greece/large/Plate");
+                }else{
+                    System.out.println("URI has unexpected format: " +  uri);
+                    continue;
+                }
+                handleUri(media, uri);
+            }
+        }
+        if (testOnly){
+            tx.setRollbackOnly();
+        }
+        app.commitTransaction(tx);
+    }
+
+       private void updateImageSizes(ICdmDataSource cdmDestination){
+        CdmApplicationController app = CdmIoApplicationController.NewInstance(cdmDestination, DbSchemaValidation.VALIDATE);
+        TransactionStatus tx = app.startTransaction();
+
+        List<Media> list = app.getMediaService().list(Media.class, null, null, null, null);
+        for (Media media : list){
+            if (! isFirstImportMedia(media)){
+                continue;
+            }
+            Set<MediaRepresentation> reps = media.getRepresentations();
+            for (MediaRepresentation rep : reps){
+                for (MediaRepresentationPart part : rep.getParts()){
+                    if (part.isInstanceOf(ImageFile.class)){
+                        handlePart(CdmBase.deproxy(part, ImageFile.class));
+                    }else{
+                        System.out.println("Representation part is not of type ImageFile: "+  part.getId());
+                    }
+                }
+            }
+        }
+
+        if (testOnly){
+            tx.setRollbackOnly();
+        }
+        app.commitTransaction(tx);
+
+       }
+
+
+
+    /**
+     * @param media
+     * @return
+     */
+    private boolean isFirstImportMedia(Media media) {
+        Boolean result = null;
+        Set<String> urls = getUrlStringForMedia(media);
+        for (String url : urls){
+            if (url.startsWith("http://150.140.202.8/files/Goula/") || url.startsWith("http://n4412.gr/images/Globula")){
+                if (result == Boolean.TRUE){
+                    System.out.println("Ambigous: "  + media.getId());
+                    return false;
+                }
+                result = false;
+            }else if (url.startsWith("https://media.e-taxonomy.eu/flora-greece")){
+                if (result == Boolean.FALSE){
+                    System.out.println("Ambigous: "  + media.getId());
+                    return false;
+                }
+                result = true;
+            }
+        }
+        if (result == null){
+            System.out.println("No data: "  + media.getId());
+            return false;
+        }
+        return result;
+    }
+
+    /**
+     * @param media
+     * @return
+     */
+    private Set<String> getUrlStringForMedia(Media media) {
+        Set<String> result = new HashSet<>();
+        for (MediaRepresentation rep : media.getRepresentations()){
+            for (MediaRepresentationPart part : rep.getParts()){
+                URI uri = part.getUri();
+                if (uri != null){
+                    result.add(uri.toString());
+                }else{
+                    System.out.println("URI is null:" + media.getId());
+                }
+            }
+        }
+        return result;
+    }
+
+    /**
+     * @param part
+     */
+    private void handlePart(ImageFile part) {
+        ImageInfo imageInfo = null;
+        URI uri = part.getUri();
+        try {
+            imageInfo = ImageInfo.NewInstance(uri, 0);
+        } catch (Exception e) {
+            String message = "An error occurred when trying to read image meta data for %s.";
+            message = String.format(message, uri.toString());
+            System.out.println(message);
+            return;
+        }
+        part.setHeight(imageInfo.getHeight());
+        part.setWidth(imageInfo.getWidth());
+
+        MediaRepresentation representation = part.getMediaRepresentation();
+        representation.setMimeType(imageInfo.getMimeType());
+        representation.setSuffix(imageInfo.getSuffix());
+
+    }
+
+    /**
+     * @param reps
+     * @param uri
+     */
+    private void handleUri(Media media, String uriStr) {
+        URI uri = URI.create(uriStr);
+        ImageInfo imageInfo = null;
+        try {
+            imageInfo = ImageInfo.NewInstance(uri, 0);
+        } catch (Exception e) {
+            String message = "An error occurred when trying to read image meta data for %s.";
+            message = String.format(message, uri.toString());
+            System.out.println(message);
+        }
+        ImageFile imageFile = ImageFile.NewInstance(uri, null, imageInfo);
+
+        MediaRepresentation representation = MediaRepresentation.NewInstance();
+
+        if(imageInfo != null){
+            representation.setMimeType(imageInfo.getMimeType());
+            representation.setSuffix(imageInfo.getSuffix());
+        }
+        representation.addRepresentationPart(imageFile);
+        media.addRepresentation(representation);
+
+    }
+
+
+
+
+    /**
+        * @param args
+        */
+       public static void main(String[] args) {
+               GreeceLargeImagesAdderActivator me = new GreeceLargeImagesAdderActivator();
+//             me.addLargeImage(cdmDestination);
+               me.updateImageSizes(cdmDestination);
+//             me.test();
+               System.exit(0);
+       }
+
+}