org.ptm.translater.App.java Source code

Java tutorial

Introduction

Here is the source code for org.ptm.translater.App.java

Source

package org.ptm.translater;

import org.ptm.translater.ch1.dao.ArchiveDao;
import org.ptm.translater.ch1.domain.Archive;
import org.ptm.translater.ch1.domain.Photo;
import org.ptm.translater.ch1.domain.PhotoSize;
import org.ptm.translater.ch2.dao.PhotoDao;
import org.ptm.translater.ch2.dao.TagDao;
import org.ptm.translater.ch2.dao.UserDao;
import org.ptm.translater.ch2.domain.AppUser;
import org.ptm.translater.ch2.domain.Role;
import org.ptm.translater.ch2.domain.Tag;
import org.springframework.context.support.GenericXmlApplicationContext;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * AppUser: kmuhov
 * Date: 23.04.13
 * Time: 16:35
 * To change this template use File | Settings | File Templates.
 */
public class App {

    public static void main(String... args) {
        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
        ctx.load("file:src/main/resources/spring/datasource.xml");
        ctx.refresh();

        GenericXmlApplicationContext ctx2 = new GenericXmlApplicationContext();
        ctx2.load("file:src/main/resources/spring/datasource2.xml");
        ctx2.refresh();

        ArchiveDao archiveDao = ctx.getBean("archiveDao", ArchiveDao.class);
        List<Archive> archives = archiveDao.findAll();

        UserDao userDao = ctx2.getBean("userDao", UserDao.class);
        TagDao tagDao = ctx2.getBean("tagDao", TagDao.class);
        PhotoDao photoDao = ctx2.getBean("photoDao", PhotoDao.class);

        List<Tag> tagz = tagDao.findAll();
        Map<String, Long> hashTags = new HashMap<String, Long>();
        for (Tag tag : tagz)
            hashTags.put(tag.getName(), tag.getId());

        MongoCache cache = new MongoCache();
        Calendar calendar = Calendar.getInstance();

        Map<String, String> associates = new HashMap<String, String>();

        for (Archive archive : archives) {
            AppUser appUser = new AppUser();
            appUser.setName(archive.getName());
            appUser.setEmail(archive.getUid() + "@mail.th");
            appUser.setPassword("123456");

            Role role = new Role();
            role.setRoleId("ROLE_USER");
            appUser.setRole(role);

            userDao.save(appUser);
            System.out.println("\tCreate user " + appUser);

            for (Photo photo : archive.getPhotos()) {
                // ?  ??? 
                if (cache.contains(photo.getUid()))
                    continue;

                System.out.println("\tNew photo");
                org.ptm.translater.ch2.domain.Photo photo2 = new org.ptm.translater.ch2.domain.Photo();
                photo2.setAppUser(appUser);
                photo2.setName(photo.getTitle());
                photo2.setLicense((byte) 7);
                photo2.setDescription(photo.getDescription());

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    calendar.setTime(sdf.parse(photo.getTaken()));

                    if (calendar.get(Calendar.YEAR) != 0 && calendar.get(Calendar.YEAR) > 1998)
                        continue;
                    photo2.setYear(calendar.get(Calendar.YEAR));
                    photo2.setMonth(calendar.get(Calendar.MONTH) + 1);
                    photo2.setDay(calendar.get(Calendar.DAY_OF_MONTH));
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

                if (photo.getLongitude() != null && photo.getLongitude().length() > 0) {
                    //                    String key = photo.getLongitude()+"#"+photo.getLatitude();
                    photo2.setLatitude(photo.getLatitude());
                    photo2.setLongitude(photo.getLongitude());
                    //                    if (associates.containsKey(key)) {
                    //                        photo2.setAddress(associates.get(key));
                    //                    } else {
                    //                        Geocoder geocoder = new Geocoder();
                    //                        GeocoderRequestBuilder geocoderRequest = new GeocoderRequestBuilder();
                    //                        GeocoderRequest request =
                    //                            geocoderRequest.setLocation(new LatLng(photo.getLongitude(), photo.getLatitude())).getGeocoderRequest();
                    //
                    //                        GeocodeResponse response = geocoder.geocode(request);
                    //                        if (response.getResults().size() > 0) {
                    //                            photo2.setAddress(response.getResults().get(0).getFormattedAddress());
                    //                        }
                    //                        try { Thread.sleep(2000); } catch (InterruptedException ex) { ex.printStackTrace(); }
                    //                    }
                }

                System.out.println("\tFind tags");
                Set<Tag> tags = new HashSet<Tag>();
                for (org.ptm.translater.ch1.domain.Tag tag : photo.getTags()) {
                    Tag item = new Tag();
                    item.setName(tag.getName());
                    if (hashTags.containsKey(tag.getName())) {
                        item.setId(hashTags.get(tag.getName()));
                    } else {
                        tagDao.save(item);
                        hashTags.put(item.getName(), item.getId());
                    }
                    System.out.println("\t\tinit tag " + tag.getName());
                    tags.add(item);
                }
                photo2.setTags(tags);
                System.out.println("\tFind " + tags.size() + " tags");
                photoDao.save(photo2);
                System.out.println("\tSave photo");

                Imaginator img = new Imaginator();
                img.setFolder(photo2.getId().toString());
                img.setPath();

                for (PhotoSize ps : photo.getSizes()) {
                    if (ps.getLabel().equals("Original")) {
                        img.setImage(ps.getSource());
                        break;
                    }
                }
                img.generate();
                System.out.println("\tGenerate image of photo");
                img = null;
                cache.create(photo.getUid());
                cache.create(photo2);

                System.out.println("Generate: " + photo2);
            }
        }
    }
}

//1271 klnerdom
//1271 kolnerdom