Rev 473 | Rev 580 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.util;import java.io.BufferedOutputStream;import java.io.BufferedWriter;import java.io.File;import java.io.FileWriter;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.StringTokenizer;import in.shop2020.metamodel.core.Entity;import in.shop2020.metamodel.core.FreeformContent;import in.shop2020.metamodel.core.Media;import in.shop2020.metamodel.core.Slide;import in.shop2020.metamodel.definitions.Catalog;import in.shop2020.metamodel.definitions.DefinitionsContainer;import in.shop2020.metamodel.definitions.EntityContainer;import in.shop2020.metamodel.util.CreationUtils;//import in.shop2020.metamodel.util.OldCreationUtils;/** @author rajveer***/public class ContentMigrator {private String sourceDbPath;private String destinationDbPath;public static void main(String[] args) throws Exception {String usage = "Usage: ContentMigrator {source db path}{destination db path}";String sourceDbPath = null, destinationDbPath = null;if(args.length < 2) {System.out.println(usage);System.exit(-1);}else{sourceDbPath = args[0];destinationDbPath = args[1];}ContentMigrator contentmigrator = new ContentMigrator(sourceDbPath, destinationDbPath);boolean result = contentmigrator.migrate();System.out.println("Content migration status: "+ result);}public ContentMigrator(String sourceDbPath, String destinationDbPath) {this.sourceDbPath = sourceDbPath;this.destinationDbPath = destinationDbPath;}/*** this function will read source definition and source entities, and will convert* source entities to destination entities according to destination definitions.** @return boolean* @throws Exception*/public boolean migrate() throws Exception{/**DefinitionsContainer sourceDefs = new DefinitionsContainer(sourceDbPath);DefinitionsContainer destinationDefs = new DefinitionsContainer(destinationDbPath);EntityContainer sourceEnts = new EntityContainer(sourceDbPath);EntityContainer destinationEnts = new EntityContainer(destinationDbPath);Map<Long, Entity> entities = sourceEnts.getEntities();File f = new File("/home/rajveer/Desktop/info.txt");FileWriter fstream = new FileWriter(f);BufferedWriter out = new BufferedWriter(fstream);StringBuilder sb = new StringBuilder();for(Entity entity: entities.values()){List<Slide> slides = entity.getSlides();//List<String> vidLabels = CreationUtils.getMediaLabels(entity.getID(),"youtube");//List<String> imgLabels = CreationUtils.getMediaLabels(entity.getID(),"image");//Map<String, Media> rawMedia = CreationUtils.getRawMedia(entity.getID());// List<String> vidLabels = OldCreationUtils.getMediaLabels(entity.getID(),"youtube");// List<String> imgLabels = OldCreationUtils.getMediaLabels(entity.getID(),"image");Entity newEntity = new Entity(entity.getID(), entity.getCategoryID());newEntity.setBrand(entity.getBrand());newEntity.setModelName(entity.getModelName());newEntity.setModelNumber(entity.getModelNumber());Map<String, in.shop2020.creation.util.Media> rawMedia = OldCreationUtils.getRawMedia(entity.getID());Map<String, Media> newrawMedia = new HashMap<String, Media>();int totalRawVideos = 0;int totalRawImages = 0;if(rawMedia!=null){for(String label: rawMedia.keySet()){in.shop2020.creation.util.Media media = rawMedia.get(label);label = media.getLabel();String type = media.getType();String location = media.getLocation();Media newMedia = new Media(label, type, location);newMedia.setTitle("Title");if(type.equals("image")){totalRawImages++;newMedia.setFileName(media.getFileName());newMedia.setVideoType("");}else{totalRawVideos++;newMedia.setVideoType("withoutskin");newMedia.setFileName("");}newrawMedia.put(label, newMedia);System.out.println("old " + media.toString());// System.out.println("new " + newMedia.toString());// OldCreationUtils.removeMedia(entity.getID(), label);CreationUtils.addMedia(entity.getID(), newMedia);}}int totalVideos = 0;int totalImages = 0;for(Slide slide: slides ){FreeformContent ffc = slide.getFreeformContent();List<String> vidList = new ArrayList<String>();List<String> imgList = new ArrayList<String>();if(ffc!=null){List<String> vidFfcLabels = ffc.getYoutubeRefs();if(vidFfcLabels!=null){for(String vidFfcLabel: vidFfcLabels){StringTokenizer strTkn = new StringTokenizer(vidFfcLabel,"~!~");String vidLabel = null;if(strTkn.hasMoreTokens()){vidLabel = strTkn.nextToken();vidList.add(vidFfcLabel);// ffc.removeMedia("youtube", vidFfcLabel);// ffc.addMedia("youtube", vidLabel);}if(vidLabel == null){System.out.println("Something is really breaking .. bu ha ha !!!");}totalVideos++;}}List<String> imgFfcLabels = ffc.getImageRefs();if(imgFfcLabels!=null){for(String imgFfcLabel: imgFfcLabels){StringTokenizer strTkn = new StringTokenizer(imgFfcLabel,"~!~");String imgLabel = null;if(strTkn.hasMoreTokens()){imgLabel = strTkn.nextToken();imgList.add(imgFfcLabel);// ffc.removeMedia("image", imgFfcLabel);// ffc.addMedia("image", imgLabel);}if(imgLabel == null){System.out.println("Something is really breaking .. bu ha ha !!!");}totalImages++;}}for(String vidlab : vidList){ffc.removeMedia("youtube", vidlab);ffc.addMedia("youtube", (new StringTokenizer(vidlab, "~!~")).nextToken());}for(String imglab : imgList){ffc.removeMedia("image",imglab);ffc.addMedia("image", (new StringTokenizer(imglab, "~!~")).nextToken());}slide.setFreeformContent(ffc);System.out.println("Slide id is " + slide.getSlideDefinitionID() + " vid labels: " + vidFfcLabels + " img labels" + imgFfcLabels);}newEntity.addSlide(slide);}System.out.println("entity:"+ entity.getID() + ":trv:" + totalRawVideos + ":tri:" + totalRawImages +":tv:" + totalVideos +":ti:" + totalImages);sb.append("entity:"+ entity.getID() + ":trv:" + totalRawVideos + ":tri:" + totalRawImages +":tv:" + totalVideos +":ti:" + totalImages+ ":tr:" +(totalRawVideos+totalRawImages) + ":t:" +(totalVideos+totalImages) + ":vd:" +(totalRawVideos-totalVideos) + ":id:" +(totalRawImages-totalImages) + "\n");destinationEnts.updateEntity(newEntity);CreationUtils.storeEntity(newEntity);Map<Long, List<Entity>> entitiesByCategory =sourceEnts.getEntitiesbyCategory();Map<Long, Entity> xentities = sourceEnts.getEntities();CreationUtils.rewriteRepository(xentities, entitiesByCategory);}out.write(sb.toString());out.close();*/return true;}}