Java tutorial
/* * 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 juan; import Javi.Category; import Javi.Image; import Javi.Itag; import java.util.ArrayList; import java.util.Iterator; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.MongoClient; import java.net.UnknownHostException; /** * * @author inftel10 */ public class Estructuraimagen { private ArrayList<ImageMongo> imagesMongo; public Estructuraimagen() { this.imagesMongo = null; } public ArrayList<ImageMongo> getImagesMongo() { return imagesMongo; } public void setImagesMongo(ArrayList<ImageMongo> imagesMongo) { this.imagesMongo = imagesMongo; } public void pasaraestructura(ArrayList<Image> images) { Itag tag = null; if (images.size() > 0) { imagesMongo = new ArrayList<ImageMongo>(); } //recuperamos imagenes Iterator<Image> nombreIterator = images.iterator(); while (nombreIterator.hasNext()) { ImageMongo imagen = null; ArrayList<Itag> itags = null; Image elem = nombreIterator.next(); if (elem.getCategories() != null) { //recuperamos categorias Iterator<Category> nombreItcat = elem.getCategories().iterator(); while (nombreItcat.hasNext()) { Category elemcat = nombreItcat.next(); //recuperamos tags Iterator<Itag> nombreIttag = elemcat.getTags().iterator(); while (nombreIttag.hasNext()) { Itag elemtag = nombreIttag.next(); //comparamos los tags switch (elemtag.getName()) { case "Image Height": String height = elemtag.getValue(); int lh = height.length(); lh = lh - 7; height = height.substring(0, lh); tag = new Itag(-1, "Image Height", height); if (itags == null) { itags = new ArrayList<Itag>(); } itags.add(tag); break; case "Image Width": String width = elemtag.getValue(); int lw = width.length(); lw = lw - 7; width = width.substring(0, lw); tag = new Itag(-1, "Image Width", width); if (itags == null) { itags = new ArrayList<Itag>(); } itags.add(tag); break; case "Make": tag = new Itag(-1, "Make", elemtag.getValue()); if (itags == null) { itags = new ArrayList<Itag>(); } itags.add(tag); break; case "Date/Time": String fecha = elemtag.getValue(); fecha = fecha.substring(0, 4); tag = new Itag(-1, "Date/Time", fecha); if (itags == null) { itags = new ArrayList<Itag>(); } itags.add(tag); break; } } } String tamano = elem.getSize(); int l = tamano.length(); l = l - 2; tamano = tamano.substring(0, l); imagen = new ImageMongo(-1, elem.getRute(), elem.getName(), elem.getExtension(), tamano, itags); imagesMongo.add(imagen); } } } public void intoMongo(ArrayList<ImageMongo> imagesMongo) throws UnknownHostException { int imheight = 0; int imwidth = 0; String immake = ""; int imdatetime = 0; int tamano = 0; //conectamos a la base de datos MongoClient mongoClient; mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("mydb"); //BORRAMOS LA BASE DE DATOS PARA QUE NO HAYA ELEMENTOS DUPLICADOS db.dropDatabase(); DBCollection modfecha = db.getCollection("modelo-fecha"); DBCollection modtam = db.getCollection("modelo-tamao"); DBCollection modres = db.getCollection("modelo-resolucion"); DBCollection tamfecha = db.getCollection("tamano-fecha"); //recuperamos imagenes Iterator<ImageMongo> nombreIterator = imagesMongo.iterator(); BasicDBObject docmf1; BasicDBObject docmt1; BasicDBObject docmr1; BasicDBObject doctf1; while (nombreIterator.hasNext()) { ImageMongo elem = nombreIterator.next(); if (elem.getTags() != null) { Iterator<Itag> nombreItag = elem.getTags().iterator(); imheight = 0; imwidth = 0; immake = ""; imdatetime = 0; while (nombreItag.hasNext()) { Itag elemtag = nombreItag.next(); switch (elemtag.getName()) { case "Image Height": imheight = Integer.parseInt(elemtag.getValue()); break; case "Image Width": imwidth = Integer.parseInt(elemtag.getValue()); break; case "Make": immake = elemtag.getValue(); break; case "Date/Time": imdatetime = Integer.parseInt(elemtag.getValue()); break; } } tamano = Integer.parseInt(elem.getSize()); //insertamos un docuemnto en la coleccion docmf1 = new BasicDBObject(); docmf1.put("nombre", elem.getName()); docmf1.put("fecha", imdatetime); docmf1.put("ruta", elem.getRute().getName()); docmf1.put("marca", immake); modfecha.insert(docmf1); docmt1 = new BasicDBObject(); docmt1.put("nombre", elem.getName()); docmt1.put("tamano", tamano); docmt1.put("ruta", elem.getRute().getName()); docmt1.put("marca", immake); modtam.insert(docmt1); docmr1 = new BasicDBObject(); docmr1.put("nombre", elem.getName()); docmr1.put("height", imheight); docmr1.put("width", imwidth); docmr1.put("ruta", elem.getRute().getName()); docmr1.put("marca", immake); modres.insert(docmr1); doctf1 = new BasicDBObject(); doctf1.put("nombre", elem.getName()); doctf1.put("fecha", imdatetime); doctf1.put("ruta", elem.getRute().getName()); doctf1.put("tamano", tamano); tamfecha.insert(doctf1); } } } }