gt:             filePath = "SWIFT"+ File.separator + "ver1";
context: /*
   * DataCruncher
   * Copyright (c) Mario Altimari. All rights reserved.
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU Affero General Public License as
   * published by the Free Software Foundation, either version 3 of the
   * License, or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU Affero General Public License for more details.
   *
   * You should have received a copy of the GNU Affero General Public License
   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   *
   */
  
  package com.datacruncher.jpa.dao;
  
  
  import com.datacruncher.constants.StreamType;
  import com.datacruncher.jpa.entity.SchemaLibEntity;
  import com.datacruncher.utils.generic.I18n;
  import com.datacruncher.jpa.Destroy;
  import com.datacruncher.jpa.ReadList;
  import com.datacruncher.jpa.ReadOnlyTx;
  
  import java.io.File;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  import javax.persistence.Query;
  
  import org.apache.log4j.Logger;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.transaction.annotation.Transactional;
  @ReadOnlyTx
  public class SchemaLibDao {
      Logger log = Logger.getLogger(this.getClass().getName());
  
      @PersistenceContext
      private EntityManager em;
  
      @Autowired
      CommonDao commonDao;
  
      protected SchemaLibDao(){}
  
  
      public Map<Integer,String> getStandardLib() {
          Map<Integer,String> mapStandlib = new HashMap<Integer,String>();
          mapStandlib.put(StreamType.HL7, "HL7");
          mapStandlib.put(StreamType.SWIFT, "SWIFT");
          mapStandlib.put(StreamType.EDI_CICA, "EDI_CICA");
          return mapStandlib;
      }
      public ReadList read(int start, int limit) {
          ReadList readList = new ReadList();
          try {
              em.clear();
              @SuppressWarnings("unchecked")
              List<Long> count = em.createNamedQuery("SchemaLibEntity.count").getResultList();
              Query query = em.createNamedQuery("SchemaLibEntity.findAll");
              if(start >= 0 && limit >= 0) {
                  query.setFirstResult(start);
                  query.setMaxResults(limit);
              }
              readList.setTotal(count.get(0));
              readList.setResults(query.getResultList());
              em.clear();
          } catch (Exception exception) {
              log.error("SchemasLibDao - read : " + exception);
              readList.setSuccess(false);
              readList.setMessage(I18n.getMessage("error.error") + "  : SchemasLibDao - read");
              return readList;
          }
          readList.setSuccess(true);
          readList.setMessage(I18n.getMessage("success.listRecord"));
          return readList;
      }
  
  
      @SuppressWarnings("unchecked")
      public ArrayList<SchemaLibEntity> findAll() {
          ArrayList<SchemaLibEntity> listLibEntity = new ArrayList<SchemaLibEntity>();
          try {
               listLibEntity = (ArrayList<SchemaLibEntity>) em.createNamedQuery("SchemaLibEntity.findAll")
                          .getResultList();
          } catch (Exception exception) {
              log.error("SchemaLibDao - findAll : " + exception);
          }
          return listLibEntity;
      }
  
      public SchemaLibEntity find(long idSchemaLib) {
          SchemaLibEntity schemaLibEntity = null;
          try {
              schemaLibEntity = em.find(SchemaLibEntity.class, idSchemaLib);
          } catch (Exception exception) {
              log.error("SchemasLibDao - find : " + exception);
          }
          return schemaLibEntity;
      }
  
      public ReadList readByLibTypeAndVersion(int libType , String version) {
          ReadList readList = new ReadList();
          try {
               readList.setResults(em.createNamedQuery("SchemaLibEntity.findByLibTypeAndVersion")
                          .setParameter("libType", libType)
                          .setParameter("version", version)
                          .getResultList());
          } catch (Exception exception) {
              log.error("SchemaLibDao - readByLibTypeAndVersion : " + exception);
              readList.setSuccess(false);
              readList.setMessage(I18n.getMessage("error.error") + "  : SchemasLibDao - readByLibTypeAndVersion");
              return readList;
          }
          readList.setSuccess(true);
          readList.setMessage(I18n.getMessage("success.listRecord"));
          return readList;
      }
      @SuppressWarnings("unchecked")
      public ReadList readByLibType(int libType) {
          ReadList readList = new ReadList();
          try {
              readList.setResults(em.createNamedQuery("SchemaLibEntity.findByLibType")
                          .setParameter("libType", libType)
                          .getResultList());
              List<SchemaLibEntity> listEntity = (List<SchemaLibEntity>)readList.getResults();
              List<SchemaLibEntity> listResults = new ArrayList<SchemaLibEntity>();
              Set<String> setLibVersion = new HashSet<String>();
              for(SchemaLibEntity schemaLibEntity : listEntity) {
              	if(setLibVersion.add(schemaLibEntity.getVersion())) {
              		listResults.add(schemaLibEntity);
              	}
              }
              
              readList.setResults(listResults);
          } catch (Exception exception) {
              log.error("SchemaLibDao - readByLibType : " + exception);
              readList.setSuccess(false);
              readList.setMessage(I18n.getMessage("error.error") + "  : SchemasLibDao - readByLibType");
              return readList;
          }
          readList.setSuccess(true);
          readList.setMessage(I18n.getMessage("success.listRecord"));
          return readList;
      }
  
      @Transactional
      public void init() {
          try {
              @SuppressWarnings("unchecked")
              List<Long> result = em.createNamedQuery("SchemaLibEntity.count").getResultList();
              if (result.get(0) == 0L) {
                  Long id= initLibOpen();
                  initLibEE(id);
              }
          } catch(Exception exception) {
              log.error("SchemaLibDao - init : " + exception);
          }
      }
  
      private void insLib(Long idSchemaLib,int libType,String version,String libPath,String defaultNsLib,String libFile, int availability) {
          String libName  =  libFile ;
          try {
              insLib(idSchemaLib,libType,version,libPath,defaultNsLib,libFile,libName,availability);
          } catch(Exception exception) {
              log.error("SchemaLibDao - insLib1 : " + exception);
          }
  
      }
  
      private void insLib(Long idSchemaLib,int libType,String version,String libPath,String defaultNsLib,String libFile,String libName, int availability) {
          SchemaLibEntity schemaLibEntity;
          try {
              schemaLibEntity = new SchemaLibEntity(idSchemaLib,libType,version,libPath,defaultNsLib,libFile,libName);
              commonDao.persist(schemaLibEntity);
          } catch(Exception exception) {
              log.error("SchemaLibDao - insLib2 : " + exception);
          }
  
      }
      private long initLibOpen() {
          String filePath ;
          String defNsLib ;
          Long id = 1L;
          try {
              defNsLib = "\"urn:hl7-org:v3\"";
              filePath = "HL7"+ File.separator + "ver2010"+ File.separator +"multicacheschemas";
              insLib(id++,StreamType.HL7, "2010", filePath , defNsLib, "COMT_IN001101UV01",1);
              insLib(id++,StreamType.HL7, "2010", filePath , defNsLib, "COMT_IN001102UV01",1);
              insLib(id++,StreamType.HL7, "2010", filePath , defNsLib, "COMT_IN001103UV01",1);
  
              filePath = "HL7"+ File.separator + "ver2008"+ File.separator +"multicacheschemas";
              insLib(id++, StreamType.HL7, "2008", filePath, defNsLib, "PRPA_IN101301UV02",1);
              insLib(id++,StreamType.HL7,"2008", filePath , defNsLib, "PRPA_IN101302UV02",1);
              insLib(id++,StreamType.HL7,"2008", filePath , defNsLib, "PRPA_IN101303UV02",1);
  
  