Java tutorial
/* * Copyright (c) 2014 Haulmont Technology Ltd. All Rights Reserved. * Haulmont Technology proprietary and confidential. * Use is subject to license terms. */ package com.haulmont.ext.core.app.loadClient; import com.haulmont.cuba.core.EntityManager; import com.haulmont.cuba.core.Persistence; import com.haulmont.cuba.core.Query; import com.haulmont.cuba.core.Transaction; import com.haulmont.cuba.core.global.Metadata; import com.haulmont.cuba.core.global.View; import com.haulmont.cuba.core.global.ViewRepository; import com.haulmont.ext.core.app.LoadClientService; import com.haulmont.ext.core.entity.*; import com.haulmont.ext.core.entity.Enum.VillaEnum; import com.haulmont.taskman.core.enums.SexEnum; import org.apache.commons.io.FileUtils; import org.aspectj.util.FileUtil; import org.springframework.stereotype.Service; import javax.inject.Inject; import javax.persistence.NoResultException; import java.io.*; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; import java.util.UUID; /** * Created by mahdi on 1/20/14. */ @Service(LoadClientService.NAME) public class LoadClientServiceBean implements LoadClientService { @Inject Persistence persistance; @Inject Metadata metadata; @Override public void loadFromFile(File file) throws IOException { /* InputStreamReader inputStream = new FileReader(path); BufferedReader bufferedReader; bufferedReader = new BufferedReader(inputStream); String line = ""; List<String[]> lines = new ArrayList<String[]>(); while ((line = bufferedReader.readLine()) != null) { lines.add(line.split(";")); } */ List<String> lines = FileUtils.readLines(file); List<String[]> splitLines = new ArrayList<String[]>(); for (String l : lines) { splitLines.add(l.split(";")); } createSuperTransaction(splitLines); } // ??-? // ??-? ??? // ? ??, ???? private void createSuperTransaction(List<String[]> lines) { Republic republic = null; Aread aread = null; Villa villa = null; CompanyExt companyExt = null; for (String[] l : lines) { String republicS = l[0]; if (!republicS.equals("")) republic = findRepublic(republicS); String areadS = l[1]; if (!areadS.equals("")) { aread = findAread(areadS); if (aread == null) aread = createAread(areadS, republic); } String[] villaS = new String[3]; villaS[0] = l[2]; villaS[1] = l[3]; villaS[2] = l[4]; if (!villaS[0].equals("") || !villaS[2].equals("")) { villa = findVilla(villaS, republic); if (villa == null) { if (!villaS[0].equals("")) villa = createVilla(villaS, republic, aread); if (!villaS[2].equals("")) villa = createVilla(villaS, republic, null); } } String[] companyS = new String[6]; try { companyS[0] = l[5]; companyS[1] = l[6]; companyS[2] = l[7]; companyS[3] = l[8]; companyS[4] = l[9]; companyS[5] = l[10]; } catch (Exception e) { if (companyS[2] == null) { companyS[2] = ""; } // ... } companyExt = findCompany(companyS[1]); if (companyExt == null) { if (!companyS[1].equals("") && companyS[2].equals("")) companyExt = createCompany(companyS, villa, null); if (!companyS[1].equals("") && !companyS[2].equals("")) companyExt = createCompany(companyS, villa, findCompany(companyS[2])); } String[] clientS = new String[l.length]; for (int i = 0; i + 11 < l.length; i++) { clientS[i] = l[i + 11]; } createClient(clientS, companyExt); clientS.toString(); } } //? ? private Republic findRepublic(String line) { Transaction tx = persistance.createTransaction(); Republic republic = null; try { EntityManager em = persistance.getEntityManager(); Query query = em.createQuery("select r from ext$Republic r where r.regionname = ?1"); query.setParameter(1, line); republic = (Republic) query.getSingleResult(); tx.commit(); } finally { tx.end(); } return republic; } //? private Aread findAread(String line) { Transaction tx = persistance.createTransaction(); Aread aread = null; try { try { EntityManager em = persistance.getEntityManager(); Query query = em.createQuery("select a from ext$Aread a where a.areadname =?1"); query.setParameter(1, line); aread = (Aread) query.getSingleResult(); tx.commit(); } catch (NoResultException e) { tx.end(); return null; } } finally { tx.end(); } return aread; } // ? - , ? ?, ? ? ? ?? private Aread createAread(String line, Republic republic) { Transaction tx = persistance.createTransaction(); Aread aread = new Aread(); try { EntityManager em = persistance.getEntityManager(); aread.setAreadname(line); aread.setRepublic(republic); em.persist(aread); tx.commit(); } finally { tx.end(); } return aread; } //? ?? private Villa findVilla(String[] lineS, Republic republic) { Transaction tx = persistance.createTransaction(); Villa villa = null; try { try { ViewRepository viewRepository = metadata.getViewRepository(); EntityManager em = persistance.getEntityManager(); Query query = em .createQuery("select v from ext$Villa v where v.villaname =?1 and v.republic.id = ?2") .addView(viewRepository.getView(Villa.class, "edit")); if (!lineS[0].equals("")) query.setParameter(1, lineS[0]); else query.setParameter(1, lineS[2]); query.setParameter(2, republic); villa = (Villa) query.getSingleResult(); tx.commit(); } catch (NoResultException e) { tx.end(); return null; } } finally { tx.end(); } return villa; } // ? - ?? , ? ? , ? ? ? ?? private Villa createVilla(String[] lineS, Republic republic, Aread aread) { Transaction tx = persistance.createTransaction(); Villa villa = new Villa(); try { EntityManager em = persistance.getEntityManager(); if (aread != null) { villa.setRepublic(republic); villa.setAread(aread); villa.setTypeVilla(VillaEnum.VILLAGE); villa.setVillaname(lineS[0]); if (!lineS[1].equals("")) villa.setAreaCenter(true); } else { villa.setRepublic(republic); villa.setTypeVilla(VillaEnum.CITY); villa.setVillaname(lineS[2]); } em.persist(villa); tx.commit(); } finally { tx.end(); } return villa; } // ? - ? , ? ?, , ? private CompanyExt createCompany(String[] lineS, Villa villa, CompanyExt parentCompanyExt) { Transaction tx = persistance.createTransaction(); CompanyExt companyExt = new CompanyExt(); try { EntityManager em = persistance.getEntityManager(); try { companyExt.setFullName(lineS[0]); companyExt.setName(lineS[1]); companyExt.setPostalAddress(lineS[3]); companyExt.setLegalAddress(lineS[4]); companyExt.setOkpo(lineS[5]); } catch (Exception e) { // , ... } companyExt.setRepublic(villa.getRepublic()); if (villa.getAread() != null) companyExt.setAread(villa.getAread()); if (villa != null) companyExt.setVilla(villa); if (parentCompanyExt != null) companyExt.setParentCompanyExt(parentCompanyExt); em.persist(companyExt); tx.commit(); } finally { tx.end(); } return companyExt; } //? ? , ?, private CompanyExt findCompany(String strCompanyExt) { Transaction tx = persistance.createTransaction(); CompanyExt companyExt; try { EntityManager em = persistance.getEntityManager(); Query query = em.createQuery("select c from ext$CompanyExt c where c.name = ?1"); query.setParameter(1, strCompanyExt); if (query.getResultList().size() != 0) companyExt = (CompanyExt) query.getResultList().get(0); else companyExt = null; tx.commit(); } finally { tx.end(); } return companyExt; } private ExtClient findClient(CompanyExt companyExt) { Transaction tx = persistance.createTransaction(); ExtClient extClient; try { EntityManager em = persistance.getEntityManager(); Query query = em.createQuery("select c from ext$Client c where c.extCompany.id = ?1"); query.setParameter(1, companyExt.getUuid()); if (query.getResultList().size() != 0) extClient = (ExtClient) query.getResultList().get(0); else extClient = null; tx.commit(); } finally { tx.end(); } return extClient; } // ? - , ? ? private void createClient(String[] lineS, CompanyExt companyExt) { Transaction tx = persistance.createTransaction(); /* ExtClient extClient = findClient(companyExt); if (extClient == null) extClient = new ExtClient(); */ ExtClient extClient = new ExtClient(); try { EntityManager em = persistance.getEntityManager(); try { extClient.setExtCompany(companyExt); extClient.setSurnameC(lineS[0]); extClient.setNameC(lineS[1]); extClient.setPatronymicC(lineS[2]); if (!lineS[1].equals("") && !lineS[2].equals("")) extClient.setName( lineS[0] + " " + lineS[1].substring(0, 1) + ". " + lineS[2].substring(0, 1) + "."); else if (!lineS[1].equals("") && lineS[2].equals("")) extClient.setName(lineS[0] + " " + lineS[1]); else if (lineS[1].equals("") && !lineS[2].equals("")) extClient.setName(lineS[0] + " " + lineS[2]); else extClient.setName(lineS[0]); extClient.setPost(lineS[3]); extClient.setNumberTel(lineS[4]); extClient.setNumberTw(lineS[5]); extClient.setPlaceBirth(lineS[6]); extClient.setCountChs(lineS[7]); if (lineS[8] == "") extClient.setSex(SexEnum.FEMALE); else extClient.setSex(SexEnum.MALE); extClient.setSurnameR(lineS[9]); extClient.setNameR(lineS[10]); extClient.setPatronymicR(lineS[11]); extClient.setPostR(lineS[12]); extClient.setBank(lineS[13]); extClient.setInn(lineS[14]); extClient.setKpp(lineS[15]); extClient.setRs(lineS[16]); extClient.setKs(lineS[17]); extClient.setBik(lineS[18]); extClient.setLs(lineS[19]); extClient.setTreasuryDepartment(lineS[20]); } catch (Exception e) { //? ? , ?? ... } // em.merge(extClient); em.persist(extClient); tx.commit(); } finally { tx.end(); } } }