exifIndexer.MetadataReader.java Source code

Java tutorial

Introduction

Here is the source code for exifIndexer.MetadataReader.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package exifIndexer;

import JSON.JSONArray;
import JSON.ResultSetConverter;
import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.util.JSON;
import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import static java.util.Objects.isNull;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.io.FilenameUtils;

/**
 *
 * @author EduardROckerse
 */
public class MetadataReader {

    public MetadataReader(String path, boolean is_recursive) {

        //Statement st = null;
        walk(path, is_recursive);

        try {
            mongoCreator();
        } catch (UnknownHostException ex) {
            Logger.getLogger(MetadataReader.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private static void print(Metadata metadata) {
        // Iterate over the data and print to System.out
        //
        // A Metadata object contains multiple Directory objects
        //
        for (Directory directory : metadata.getDirectories()) {
            // Aadir directory si no existe, sino nada.
            //
            // Each Directory stores values in Tag objects
            //
            directory.getTags().stream().forEach((tag) -> {
                //System.out.println(tag);
                System.out.println(
                        "[" + directory.getName() + "]  " + tag.getTagName() + " - " + tag.getDescription());
            });

            //
            // Each Directory may also contain error messages
            //
            if (directory.hasErrors()) {
                for (String error : directory.getErrors()) {
                    System.err.println("ERROR: " + error);
                }
            }
        }
    }

    public static void walk(String path, boolean is_recursive) {

        File root = new File(path);
        File[] list = root.listFiles();
        String filePath;
        String fileName;
        String fileExt;
        String valueName;
        String tagName;
        String catName;
        Metadata metadata;
        long fileSize;
        long fileLastModified;
        java.util.Date utilDate;
        java.sql.Date sqlDate;

        String sql = "{ ? = call INSERTIMAGE(?,?,?,?,?) }";
        String sqlMetaData = "{ call INSERTMETADATA (?,?,?,?) }";

        CallableStatement statement;
        CallableStatement statementMeta;
        long result;

        if (list == null) {
            return;
        }

        for (File f : list) {
            if (f.isDirectory() && is_recursive) {
                walk(f.getAbsolutePath(), true);
            } else {

                filePath = FilenameUtils.getFullPath(f.getAbsolutePath());
                fileName = FilenameUtils.getBaseName(f.getName());
                fileExt = FilenameUtils.getExtension(f.getName());
                utilDate = new java.util.Date(f.lastModified());
                sqlDate = new java.sql.Date(utilDate.getTime());

                fileSize = f.length();

                try {
                    metadata = ImageMetadataReader.readMetadata(f.getAbsoluteFile());
                    try {
                        DBHandler db = new DBHandler();
                        db.openConnection();
                        Connection con = db.getCon();
                        // llamada al metodo insertar imagen SQL con (filePath,fileName,fileExtension,fileSize, fileLastModified)
                        statement = con.prepareCall(sql);
                        statement.setString(2, filePath);
                        statement.setString(3, fileName);
                        statement.setString(4, fileExt);
                        statement.setLong(5, fileSize);
                        statement.setDate(6, sqlDate);
                        statement.registerOutParameter(1, java.sql.Types.NUMERIC);
                        statement.execute();
                        result = statement.getLong(1);

                        // llamada al metodo insertar metadatos SQL con (idImg,valueName, tagName, catName)
                        for (Directory directory : metadata.getDirectories()) {
                            for (Tag tag : directory.getTags()) {

                                valueName = tag.getDescription();
                                tagName = tag.getTagName();
                                catName = directory.getName();

                                if (isNull(valueName) || isNull(tagName) || isNull(catName) || valueName.equals("")
                                        || tagName.equals("") || catName.equals("") || valueName.length() > 250
                                        || tagName.length() > 250 || catName.length() > 500) {
                                    System.out.println("Exif row omitted.");
                                    System.out.println("Omitting: [" + catName + "] " + tagName + " " + valueName);
                                } else {
                                    statementMeta = con.prepareCall(sqlMetaData);
                                    statementMeta.setLong(1, result);
                                    statementMeta.setString(2, valueName);
                                    statementMeta.setString(3, tagName);
                                    statementMeta.setString(4, catName);
                                    statementMeta.executeUpdate();
                                }
                            }
                        }
                        db.closeConnection();
                    } catch (SQLException ex) {
                        System.err.println("Error with SQL command. \n" + ex);
                    }
                } catch (ImageProcessingException e) {
                    System.out.println("ImageProcessingException " + e);
                } catch (IOException e) {
                    System.out.println("IOException " + e);
                }

            }

        }

    }

    private void mongoCreator() throws UnknownHostException {
        DBHandler db = new DBHandler();
        MongoHandler dbmon = new MongoHandler();

        DB dbmongo = dbmon.connect();
        dbmongo.dropDatabase();

        db.openConnection();
        Statement st;
        String cadena[];
        String cadena2;

        try {
            st = db.getCon().createStatement();

            //Coleccin por marca de cmara
            ResultSet rs = st.executeQuery(
                    "SELECT CATEGORIES.VALUE_NAME AS CAMERA_BRAND, IMAGES.IMG_PATH, IMAGES.IMG_NAME , IMAGES.EXTENSION FROM ASOCIATED, IMAGES, CATEGORIES WHERE ASOCIATED.ID_IMG = IMAGES.ID_IMG AND ASOCIATED.VALUE_NAME = CATEGORIES.VALUE_NAME AND CATEGORIES.CAT_NAME like 'Exif IFD0' and CATEGORIES.TAG_NAME like 'Make'");
            JSONArray ja;
            ja = ResultSetConverter.convert(rs);

            DBObject dbObject;
            DBCollection collection = dbmongo.getCollection("CameraBrands");

            cadena2 = (ja.toString().substring(1, ja.toString().length() - 1));

            cadena2 = cadena2.replaceAll("},", "}<<->>");

            cadena = cadena2.split("<<->>");
            System.out.println(cadena2);

            for (String lenguaje : cadena) {
                dbObject = (DBObject) JSON.parse(lenguaje);
                collection.insert(dbObject);
            }
            st = db.getCon().createStatement();

            //Coleccion modelo de cmara
            rs = st.executeQuery(
                    "SELECT CATEGORIES.VALUE_NAME AS CAMERA_MODEL, IMAGES.IMG_PATH, IMAGES.IMG_NAME , IMAGES.EXTENSION FROM ASOCIATED, IMAGES, CATEGORIES WHERE ASOCIATED.ID_IMG = IMAGES.ID_IMG AND ASOCIATED.VALUE_NAME = CATEGORIES.VALUE_NAME AND CATEGORIES.CAT_NAME like 'Exif IFD0' and CATEGORIES.TAG_NAME like 'Model'");
            ja = ResultSetConverter.convert(rs);
            collection = dbmongo.getCollection("CameraModels");

            cadena2 = (ja.toString().substring(1, ja.toString().length() - 1));

            cadena2 = cadena2.replaceAll("},", "}<<->>");

            cadena = cadena2.split("<<->>");
            System.out.println(cadena2);

            for (String lenguaje : cadena) {
                dbObject = (DBObject) JSON.parse(lenguaje);
                collection.insert(dbObject);
            }

            //Coleccion por ISO
            rs = st.executeQuery(
                    "SELECT CATEGORIES.VALUE_NAME AS ISO_VALUE, IMAGES.IMG_PATH, IMAGES.IMG_NAME , IMAGES.EXTENSION FROM ASOCIATED, IMAGES, CATEGORIES WHERE ASOCIATED.ID_IMG = IMAGES.ID_IMG AND ASOCIATED.VALUE_NAME = CATEGORIES.VALUE_NAME AND CATEGORIES.CAT_NAME like 'Exif SubIFD' and CATEGORIES.TAG_NAME like 'ISO Speed Ratings'");
            ja = ResultSetConverter.convert(rs);
            collection = dbmongo.getCollection("SearchByISO");

            cadena2 = (ja.toString().substring(1, ja.toString().length() - 1));

            cadena2 = cadena2.replaceAll("},", "}<<->>");

            cadena2 = cadena2.replaceAll("(\"ISO_VALUE\"[:])(\")([0-9]+)(\")", "$1$3");

            cadena = cadena2.split("<<->>");
            System.out.println("DDD " + cadena2);

            for (String lenguaje : cadena) {
                dbObject = (DBObject) JSON.parse(lenguaje);
                collection.insert(dbObject);
            }

            //Coleccion por Shutter Speed
            rs = st.executeQuery(
                    "SELECT CATEGORIES.VALUE_NAME AS SHUTTER_VALUE, IMAGES.IMG_PATH, IMAGES.IMG_NAME , IMAGES.EXTENSION FROM ASOCIATED, IMAGES, CATEGORIES WHERE ASOCIATED.ID_IMG = IMAGES.ID_IMG AND ASOCIATED.VALUE_NAME = CATEGORIES.VALUE_NAME AND CATEGORIES.CAT_NAME like 'Exif SubIFD' and CATEGORIES.TAG_NAME like 'Shutter Speed Value'");
            ja = ResultSetConverter.convert(rs);
            collection = dbmongo.getCollection("Shutter_Speed");

            cadena2 = (ja.toString().substring(1, ja.toString().length() - 1));

            cadena2 = cadena2.replaceAll("},", "}<<->>");

            cadena = cadena2.split("<<->>");
            System.out.println(cadena2);

            for (String lenguaje : cadena) {
                dbObject = (DBObject) JSON.parse(lenguaje);
                collection.insert(dbObject);
            }

            //Coleccion por distintas posibles Shutter Speed
            rs = st.executeQuery(
                    "SELECT DISTINCT CATEGORIES.VALUE_NAME AS SHUTTERSPEED FROM ASOCIATED, IMAGES, CATEGORIES WHERE ASOCIATED.ID_IMG = IMAGES.ID_IMG AND ASOCIATED.VALUE_NAME = CATEGORIES.VALUE_NAME AND CATEGORIES.CAT_NAME like 'Exif SubIFD' and CATEGORIES.TAG_NAME like 'Shutter Speed Value'");
            ja = ResultSetConverter.convert(rs);
            collection = dbmongo.getCollection("Possible_shutterSpeeds");

            cadena2 = (ja.toString().substring(1, ja.toString().length() - 1));

            cadena2 = cadena2.replaceAll("},", "}<<->>");

            cadena = cadena2.split("<<->>");
            System.out.println(cadena2);

            for (String lenguaje : cadena) {
                dbObject = (DBObject) JSON.parse(lenguaje);
                collection.insert(dbObject);
            }

            //Coleccion por Date created
            rs = st.executeQuery(
                    "SELECT CATEGORIES.VALUE_NAME AS DATE_VALUE, IMAGES.IMG_PATH, IMAGES.IMG_NAME , IMAGES.EXTENSION FROM ASOCIATED, IMAGES, CATEGORIES WHERE ASOCIATED.ID_IMG = IMAGES.ID_IMG AND ASOCIATED.VALUE_NAME = CATEGORIES.VALUE_NAME AND CATEGORIES.CAT_NAME like 'Exif SubIFD' and CATEGORIES.TAG_NAME like 'Date/Time Original'");
            ja = ResultSetConverter.convert(rs);
            collection = dbmongo.getCollection("Date_created");

            cadena2 = (ja.toString().substring(1, ja.toString().length() - 1));

            cadena2 = cadena2.replaceAll("},", "}<<->>");

            cadena2 = cadena2.replaceAll(
                    "(\"DATE_VALUE\"[:]\")([0-9]{4})[:]([0-9]{2})[:]([0-9]{2})([\\s][0-9]{2}[:][0-9]{2}[:][0-9]{2}\")",
                    "$1$2-$3-$4\"");

            System.out.println("ddd" + cadena2);
            cadena = cadena2.split("<<->>");
            for (String lenguaje : cadena) {
                dbObject = (DBObject) JSON.parse(lenguaje);
                collection.insert(dbObject);
            }

            //Coleccion por GPS
            rs = st.executeQuery(
                    "SELECT CATEGORIES.VALUE_NAME AS GPS_VALUE, IMAGES.ID_IMG, IMAGES.IMG_PATH, IMAGES.IMG_NAME , IMAGES.EXTENSION FROM ASOCIATED, IMAGES, CATEGORIES WHERE ASOCIATED.ID_IMG = IMAGES.ID_IMG AND ASOCIATED.VALUE_NAME = CATEGORIES.VALUE_NAME AND CATEGORIES.CAT_NAME like 'GPS%' and (CATEGORIES.TAG_NAME like 'GPS Latitude' or CATEGORIES.TAG_NAME like 'GPS Longitude' or CATEGORIES.TAG_NAME like 'GPS Latitude Ref' or CATEGORIES.TAG_NAME like 'GPS Longitude Ref')order by IMAGES.ID_IMG, CATEGORIES.TAG_NAME");
            ja = ResultSetConverter.convert(rs);
            collection = dbmongo.getCollection("GPSFotos");

            cadena2 = (ja.toString().substring(1, ja.toString().length() - 1));

            cadena2 = cadena2.replaceAll("},", "}<<->>");

            cadena = cadena2.split("<<->>");
            System.out.println(cadena2);

            for (String lenguaje : cadena) {
                dbObject = (DBObject) JSON.parse(lenguaje);
                collection.insert(dbObject);
            }
            st.close();

            System.out.println("Mongo creada");

        } catch (SQLException ex) {
            System.err.println("SQL Error.\n" + ex);
        }

    }
}