List of usage examples for org.hibernate Session clear
void clear();
From source file:de.fhdo.gui.admin.modules.terminology.Codesystems.java
License:Apache License
private void initList() { logger.debug("initList()"); try {//from ww w . j a v a 2 s . c o m West title = (West) getFellow("titleItem"); int selectedIndex = -1; // header information for list view List<GenericListHeaderType> header = new LinkedList<GenericListHeaderType>(); header.add(new GenericListHeaderType("ID", 60, "", true, "String", true, true, false, false)); header.add(new GenericListHeaderType(Labels.getLabel("name"), 0, "", true, "String", true, true, false, false)); // load data from db SessionFactory sf = HibernateUtil.getNewSessionFactory(); Session hb_session = sf.openSession(); //Session hb_session = HibernateUtil.getSessionFactory().openSession(); List<GenericListRowType> dataList = new LinkedList<GenericListRowType>(); try { if (mode == Mode.VALUESET) { ValueSet selectedVS = null; if (selectedItem instanceof ValueSet) selectedVS = (ValueSet) selectedItem; String hql = "from ValueSet order by name"; Query q = hb_session.createQuery(hql); q.setCacheable(false); q.setCacheMode(CacheMode.IGNORE); hb_session.setCacheMode(CacheMode.IGNORE); hb_session.clear(); hb_session.flush(); logger.debug("hql: " + hql); List<ValueSet> vsList = q.list(); for (int i = 0; i < vsList.size(); ++i) { ValueSet vs = vsList.get(i); GenericListRowType row = createRowFromValueSet(vs); dataList.add(row); if (selectedVS != null) { if (vs.getId().longValue() == selectedVS.getId()) selectedIndex = i; } } // set title title.setTitle(Labels.getLabel("valuesets")); } else { CodeSystem selectedCS = null; if (selectedItem instanceof CodeSystem) selectedCS = (CodeSystem) selectedItem; String hql = "from CodeSystem order by name"; Query q = hb_session.createQuery(hql); q.setCacheable(false); q.setCacheMode(CacheMode.IGNORE); hb_session.setCacheMode(CacheMode.IGNORE); hb_session.clear(); hb_session.flush(); logger.debug("hql: " + hql); List<CodeSystem> csList = q.list(); for (int i = 0; i < csList.size(); ++i) { CodeSystem cs = csList.get(i); GenericListRowType row = createRowFromCodesystem(cs); dataList.add(row); if (selectedCS != null) { if (cs.getId().longValue() == selectedCS.getId()) selectedIndex = i; } } // set title title.setTitle(Labels.getLabel("codesystems")); } } catch (Exception e) { LoggingOutput.outputException(e, this); } finally { hb_session.close(); } // initialize list Include inc = (Include) getFellow("incList"); Window winGenericList = (Window) inc.getFellow("winGenericList"); genericList = (GenericList) winGenericList; genericList.setListId("list"); genericList.setListActions(this); genericList.setButton_new(true); genericList.setButton_edit(false); genericList.setButton_delete(true); genericList.setListHeader(header); genericList.setDataList(dataList); if (selectedIndex >= 0) genericList.setSelectedIndex(selectedIndex); } catch (Exception ex) { LoggingOutput.outputException(ex, this); } initListVersion(); }
From source file:de.fhdo.gui.admin.modules.terminology.Codesystems.java
License:Apache License
private void initListVersion() { logger.debug("initListVersion()"); logger.debug("selectedItem: " + selectedItem); Include incVersions = (Include) getFellow("incListVersions"); West title = (West) getFellow("titleVersion"); if (selectedItem == null) { logger.debug("show empty message"); if (mode == Mode.VALUESET) title.setTitle(Labels.getLabel("valuesetVersion")); else/* www. j a va 2 s. co m*/ title.setTitle(Labels.getLabel("codesystemVersion")); incVersions.setSrc(null); incVersions.setSrc("/gui/templates/MessageInclude.zul?msg=" + Labels.getLabel("noSelection")); } else { logger.debug("show version list"); int selectedIndex = -1; incVersions.setSrc(null); incVersions.setSrc("/gui/templates/GenericList.zul"); try { // header information for list view List<GenericListHeaderType> header = new LinkedList<GenericListHeaderType>(); header.add(new GenericListHeaderType("ID", 60, "", true, "String", true, true, false, false)); header.add(new GenericListHeaderType(Labels.getLabel("name"), 0, "", true, "String", true, true, false, false)); header.add(new GenericListHeaderType(Labels.getLabel("status"), 80, "", true, "String", true, true, false, false)); // load data from db Session hb_session = HibernateUtil.getSessionFactory().openSession(); hb_session.setCacheMode(org.hibernate.CacheMode.IGNORE); hb_session.clear(); List<GenericListRowType> dataList = new LinkedList<GenericListRowType>(); try { if (mode == Mode.VALUESET) { // fill version list with value set versions ValueSet selectedVS = (ValueSet) selectedItem; ValueSetVersion selectedVSV = (ValueSetVersion) selectedItemVersion; String hql = "from ValueSetVersion where valueSetId=:vs_id order by name"; Query q = hb_session.createQuery(hql); q.setParameter("vs_id", selectedVS.getId()); q.setCacheable(false); q.setCacheMode(CacheMode.IGNORE); hb_session.setCacheMode(CacheMode.IGNORE); hb_session.clear(); hb_session.flush(); List<ValueSetVersion> vsList = q.list(); for (int i = 0; i < vsList.size(); ++i) { ValueSetVersion vsv = vsList.get(i); GenericListRowType row = createRowFromValueSetVersion(vsv); dataList.add(row); if (selectedVSV != null) { if (vsv.getVersionId().longValue() == selectedVSV.getVersionId()) selectedIndex = i; } } // set title title.setTitle(Labels.getLabel("valuesetVersion") + " - " + selectedVS.getName()); } else { // fill version list with code system versions CodeSystem selectedCS = (CodeSystem) selectedItem; CodeSystemVersion selectedCSV = (CodeSystemVersion) selectedItemVersion; String hql = "from CodeSystemVersion where codeSystemId=:cs_id order by name"; Query q = hb_session.createQuery(hql); q.setParameter("cs_id", selectedCS.getId()); q.setCacheable(false); q.setCacheMode(CacheMode.IGNORE); hb_session.setCacheMode(CacheMode.IGNORE); hb_session.clear(); hb_session.flush(); List<CodeSystemVersion> csList = q.list(); for (int i = 0; i < csList.size(); ++i) { CodeSystemVersion csv = csList.get(i); GenericListRowType row = createRowFromCodesystemVersion(csv); dataList.add(row); if (selectedCSV != null) { if (csv.getVersionId().longValue() == selectedCSV.getVersionId()) selectedIndex = i; } } // set title title.setTitle(Labels.getLabel("codesystemVersion") + " - " + selectedCS.getName()); } } catch (Exception e) { LoggingOutput.outputException(e, this); } finally { hb_session.close(); } // initialize list Window winGenericList = (Window) incVersions.getFellow("winGenericList"); genericListVersion = (GenericList) winGenericList; genericListVersion.setListId("listVersion"); genericListVersion.setListActions(this); genericListVersion.setButton_new(true); genericListVersion.setButton_edit(false); genericListVersion.setButton_delete(true); genericListVersion.setListHeader(header); genericListVersion.setDataList(dataList); if (selectedIndex >= 0) genericListVersion.setSelectedIndex(selectedIndex); Button button = new Button(Labels.getLabel("changeStatus") + "..."); button.addEventListener(Events.ON_CLICK, new EventListener<Event>() { public void onEvent(Event t) throws Exception { changeStatus(); } }); genericListVersion.removeCustomButtons(); genericListVersion.addCustomButton(button); } catch (Exception ex) { LoggingOutput.outputException(ex, this); } } initDetails(); }
From source file:de.fhdo.terminologie.ws.administration._export.ExportClaml.java
License:Apache License
public void createConcepts() { ListCodeSystemConceptsRequestType conceptsReq = new ListCodeSystemConceptsRequestType(); conceptsReq.setCodeSystem(this.codeSystem); conceptsReq.setLoginToken(this.request.getLoginToken()); conceptsReq.setLoadTranslation(true); if (request.getExportParameter() != null && request.getExportParameter().getDateFrom() != null) { // Datum fr Synchronisation hinzufgen conceptsReq.setCodeSystemEntity(new CodeSystemEntity()); conceptsReq.getCodeSystemEntity().setCodeSystemEntityVersions(new HashSet<CodeSystemEntityVersion>()); CodeSystemEntityVersion csev = new CodeSystemEntityVersion(); csev.setStatusVisibilityDate(request.getExportParameter().getDateFrom()); conceptsReq.getCodeSystemEntity().getCodeSystemEntityVersions().add(csev); logger.debug("Snych-Zeit: " + request.getExportParameter().getDateFrom().toString()); } else/*from w w w. j a v a2 s . c o m*/ logger.debug("keine Snych-Zeit angegeben"); //Date dateDummy = new Date(0); //Fragen ! trick damit etwas geliefert wird! //this.codeSystem.setInsertTimestamp(dateDummy); //ListCodeSystemConceptsResponseType conceptsResp = new ListCodeSystemConcepts().ListCodeSystemConcepts(conceptsReq, hb_session); ListCodeSystemConceptsResponseType conceptsResp = new ListCodeSystemConcepts() .ListCodeSystemConcepts(conceptsReq, true, ""); //!!WIESO HIER KEINE RETURN INFORMATIONS (FALSCH BENANNT) logger.debug("[ExportClaml.java] " + conceptsResp.getReturnInfos().getMessage()); //HashMap hm = new HashMap(); clamlBindingXSD.Class clazz = null; //Durchlaufen der Konzepte if (conceptsResp.getReturnInfos().getStatus() == Status.OK) { Session hb_session = HibernateUtil.getSessionFactory().openSession(); try { Iterator itEntitiy = conceptsResp.getCodeSystemEntity().iterator(); double classCount = conceptsResp.getCodeSystemEntity().size(); double aktCount = 0; int i = 0; String altCode = ""; while (itEntitiy.hasNext()) { i++; CodeSystemEntity cse = (CodeSystemEntity) itEntitiy.next(); for (CodeSystemEntityVersion csev : cse.getCodeSystemEntityVersions()) { if (csev.getCodeSystemConcepts() != null && csev.getCodeSystemConcepts().size() > 0) { CodeSystemConcept csc = csev.getCodeSystemConcepts().iterator().next(); if (csev.getStatusVisibility() == 1) { String neuCode = csc.getCode(); if (csc.getCode() == null || csc.getCode().trim().equals("")) { neuCode = i + ""; } //Status aktuallisieren aktCount++; percentageComplete = aktCount / classCount * 100.0; currentTask = neuCode; //Neuer Code (Gruppenwechsel) if (!neuCode.equals(altCode)) { altCode = neuCode; //Neue Klasse erstellen clazz = new clamlBindingXSD.Class(); createMetaData(csev, clazz, hb_session); //clazz.setCode(evt.getTerm().getCode()); clazz.setCode(neuCode); //clazz.setKind(""); TODO chapter, block, category, ... // wenn Metadata ClaML_ClassKind heit, dann ist "chapter" etc. darin gespeichert claml.getClazz().add(clazz); countExported++; //In HashMap schreiben //hm.put(csev.getVersionId(), neuCode); } // Term hinzufgen /*Rubric rubric = new Rubric(); Label label = new Label(); label.getContent().add(csc.getTerm()); rubric.getLabel().add(label); if (csc.getIsPreferred()) { rubric.setKind(RUBRICKINDS.preferred.getCode()); addRubricKind(RUBRICKINDS.preferred.getCode()); } clazz.getRubric().add(rubric);*/ if (csc.getIsPreferred()) addRubricElement(RUBRICKINDS.preferred, csc.getTerm(), clazz, csc.getCodeSystemConceptTranslations()); else addRubricElement(null, csc.getTerm(), clazz, csc.getCodeSystemConceptTranslations()); addRubricElement(RUBRICKINDS.note, csc.getDescription(), clazz, csc.getCodeSystemConceptTranslations()); // Weitere Attribute in Metadaten speichern createMetadata(METADATA_ATTRIBUTES.hints.getCode(), csc.getHints(), clazz); createMetadata(METADATA_ATTRIBUTES.meaning.getCode(), csc.getMeaning(), clazz); createMetadata(METADATA_ATTRIBUTES.termAbbrevation.getCode(), csc.getTermAbbrevation(), clazz); if (csev.getIsLeaf() != null) createMetadata(METADATA_ATTRIBUTES.isLeaf.getCode(), csev.getIsLeaf().toString(), clazz); if (csev.getMajorRevision() != null) createMetadata(METADATA_ATTRIBUTES.majorRevision.getCode(), csev.getMajorRevision().toString(), clazz); if (csev.getMinorRevision() != null) createMetadata(METADATA_ATTRIBUTES.minorRevision.getCode(), csev.getMinorRevision().toString(), clazz); if (csev.getStatusVisibility() != null) createMetadata(METADATA_ATTRIBUTES.status.getCode(), csev.getStatusVisibility().toString(), clazz); if (csev.getStatusVisibilityDate() != null) createMetadata(METADATA_ATTRIBUTES.statusDate.getCode(), csev.getStatusVisibilityDate().toString(), clazz); //createAssociation(cse, clazz, hm, rubric, csev, csc); createAssociation(cse, clazz, null, csev, csc, hb_session); hb_session.clear(); } } } } } catch (Exception ex) { logger.error("Fehler in 'createMetaData': " + ex.getLocalizedMessage()); ex.printStackTrace(); } finally { logger.debug("Schliee Hibernate-Session (ExportClaml.java)"); hb_session.close(); } } }
From source file:de.fhdo.terminologie.ws.administration._import.ImportCS_CSV.java
License:Apache License
public String importCSV(ImportCodeSystemResponseType reponse, String ipAddress) { StaticStatus.importTotal = 0;//from w w w .j a va2 s. co m StaticStatus.importCount = 0; StaticStatus.importRunning = true; StaticStatus.exportRunning = false; StaticStatus.cancel = false; String s = ""; int count = 0, countFehler = 0; CsvReader csv; try { byte[] bytes = parameter.getImportInfos().getFilecontent(); logger.debug("wandle zu InputStream um..."); InputStream is = new ByteArrayInputStream(bytes); //csv = new CsvReader("C:\\Temp\\notfallrel_diagnosen.csv"); csv = new CsvReader(is, Charset.forName("ISO-8859-1")); csv.setDelimiter(';'); csv.setTextQualifier('"'); csv.setUseTextQualifier(true); csv.readHeaders(); logger.debug("Anzahl Header: " + csv.getHeaderCount()); // Sprachen identifizieren // Metadaten identifizieren Map<Integer, String> headerTranslations = new HashMap<Integer, String>(); Map<Integer, String> headerMetadata = new HashMap<Integer, String>(); String[] header = csv.getHeaders(); for (int i = 0; i < csv.getHeaderCount(); ++i) { try { if (header[i].contains("translation_")) { //long languageID = Long.parseLong(header[i].replace("translation_", "")); //headerTranslations.put(i, languageID); String languageCD = header[i].replace("translation_", ""); headerTranslations.put(i, languageCD); } } catch (Exception e) { } try { if (header[i].contains("metadata_")) { String mdName = header[i].replace("metadata_", ""); headerMetadata.put(i, mdName); } } catch (Exception e) { } } // Hibernate-Block, Session ffnen org.hibernate.Session hb_session = HibernateUtil.getSessionFactory().openSession(); //org.hibernate.Transaction tx = hb_session.beginTransaction(); org.hibernate.Transaction tx = hb_session.beginTransaction(); try // try-catch-Block zum Abfangen von Hibernate-Fehlern { logger.debug("Create Codesystem and Version an, if not exists..."); if (createCodeSystem(hb_session) == false) { // Fehlermeldung tx.rollback(); return "CodeSystem could not be created!"; } logger.debug("analyse and save metadata..."); // Metadaten speichern Map<String, Long> headerMetadataIDs = new HashMap<String, Long>(); for (String mdText : headerMetadata.values()) { if (metaDataMap.containsKey(mdText)) { logger.debug( "Metadata exists already, add to map: " + metaDataMap.get(mdText) + ", " + mdText); headerMetadataIDs.put(mdText, (Long) metaDataMap.get(mdText)); continue; } MetadataParameter mp = new MetadataParameter(); mp.setParamName(mdText); mp.setCodeSystem(parameter.getCodeSystem()); hb_session.save(mp); headerMetadataIDs.put(mdText, mp.getId()); logger.debug("save metadata parameter: " + mdText + " with Codesystem-ID: " + mp.getCodeSystem().getId() + ", MP-ID: " + mp.getId()); } Map<String, RelationMapType> relationMap = new HashMap<String, RelationMapType>(); while (csv.readRecord()) { if (StaticStatus.cancel) break; CreateConceptAssociationRequestType requestAssociation = null; CreateConceptRequestType request = new CreateConceptRequestType(); request.setLoginToken(parameter.getLoginToken()); request.setCodeSystem(parameter.getCodeSystem()); request.setCodeSystemEntity(new CodeSystemEntity()); request.getCodeSystemEntity() .setCodeSystemEntityVersions(new HashSet<CodeSystemEntityVersion>()); CodeSystemConcept csc = new CodeSystemConcept(); csc.setCode(csv.get("code")); csc.setIsPreferred(true); csc.setTerm(csv.get("term")); csc.setTermAbbrevation(csv.get("term_abbrevation")); logger.debug("Code: " + csc.getCode() + ", Term: " + csc.getTerm()); // Weitere Attribute prfen String s_temp; s_temp = csv.get("description"); if (s_temp != null && s_temp.length() > 0) csc.setDescription(s_temp); s_temp = csv.get("is_preferred"); if (s_temp != null) { if (s_temp.equals("1") || s_temp.equals("true")) csc.setIsPreferred(true); else csc.setIsPreferred(false); } boolean membershipChanged = false; CodeSystemVersionEntityMembership membership = new CodeSystemVersionEntityMembership(); s_temp = csv.get("is_axis"); if (s_temp != null && (s_temp.equals("1") || s_temp.equals("true"))) { membership.setIsAxis(true); membershipChanged = true; } s_temp = csv.get("is_mainclass"); if (s_temp != null && (s_temp.equals("1") || s_temp.equals("true"))) { membership.setIsMainClass(true); membershipChanged = true; } if (membershipChanged) { request.getCodeSystemEntity().setCodeSystemVersionEntityMemberships( new HashSet<CodeSystemVersionEntityMembership>()); request.getCodeSystemEntity().getCodeSystemVersionEntityMemberships().add(membership); } // check relation s_temp = csv.get("relation"); if (s_temp != null && s_temp.length() > 0) { if (relationMap.containsKey(s_temp) == false) { // check code from other code systems and add it to map logger.debug("find code from other codesystem (cross mapping) for code: " + s_temp); long crossmappingCsvId = 0; String s_temp2 = csv.get("crossmapping_csv_id"); if (s_temp2 != null) { crossmappingCsvId = Integer.parseInt(s_temp2); } if (crossmappingCsvId > 0) { logger.debug("search code: " + s_temp + ", csvId: " + crossmappingCsvId); String hql = "select distinct csc from CodeSystemConcept csc" + " join fetch csc.codeSystemEntityVersion csev" + " join fetch csev.codeSystemEntity cse" + " join cse.codeSystemVersionEntityMemberships csvem" + " join csvem.codeSystemVersion csv" + " where csc.code=:code and csv.versionId=" + crossmappingCsvId + " and csc.isPreferred=1"; /*select * from code_system_concept AS csc join code_system_entity_version AS csev ON csc.codeSystemEntityVersionId=csev.versionId join code_system_entity AS cse ON cse.id=csev.codeSystemEntityId join code_system_version_entity_membership AS csvem ON csvem.codeSystemEntityId=cse.id join code_system_version AS csv ON csv.versionId=csvem.codeSystemVersionId where code="A01.0" and csv.versionId=158*/ logger.debug("HQL: " + hql); Query q = hb_session.createQuery(hql); q.setParameter("code", s_temp); //q.setParameter("csvId", crossmappingCsvId); List<CodeSystemConcept> cscList = q.list(); logger.debug("list size: " + cscList.size()); for (CodeSystemConcept cscTemp : cscList) { RelationMapType newMapEntry = new RelationMapType( cscTemp.getCodeSystemEntityVersion().getCodeSystemEntity().getId(), cscTemp.getCodeSystemEntityVersion().getVersionId()); relationMap.put(cscTemp.getCode(), newMapEntry); logger.debug("found cross mapping with entityVersionId: " + cscTemp.getCodeSystemEntityVersion().getVersionId()); break; } logger.debug("search finished"); } } if (relationMap.containsKey(s_temp)) { logger.debug("found code in relationMap: " + s_temp + ", assign relation now"); RelationMapType mapEntry = relationMap.get(s_temp); // Es gibt eine Beziehung zu einem anderen Term requestAssociation = new CreateConceptAssociationRequestType(); requestAssociation.setLoginToken(parameter.getLoginToken()); requestAssociation.setCodeSystemEntityVersionAssociation( new CodeSystemEntityVersionAssociation()); requestAssociation.getCodeSystemEntityVersionAssociation() .setCodeSystemEntityVersionByCodeSystemEntityVersionId1( new CodeSystemEntityVersion()); requestAssociation.getCodeSystemEntityVersionAssociation() .getCodeSystemEntityVersionByCodeSystemEntityVersionId1() .setVersionId(mapEntry.getEntityVersionID()); s_temp = csv.get("association_kind"); if (s_temp != null) requestAssociation.getCodeSystemEntityVersionAssociation() .setAssociationKind(Integer.parseInt(s_temp)); else requestAssociation.getCodeSystemEntityVersionAssociation().setAssociationKind(2); s_temp = csv.get("association_type"); if (s_temp != null) { String s_temp2 = csv.get("association_type_reverse"); String reverse = ""; if (s_temp2 != null) reverse = s_temp2; requestAssociation.getCodeSystemEntityVersionAssociation() .setAssociationType(CreateAssociationType(s_temp, reverse, ipAddress)); } } else { logger.debug("code could not be found in relationMap, do not assign link"); } } // Sprachen prfen if (headerTranslations != null && headerTranslations.size() > 0) { csc.setCodeSystemConceptTranslations(new HashSet<CodeSystemConceptTranslation>()); Set<Integer> spalten = headerTranslations.keySet(); Iterator<Integer> itSpalten = spalten.iterator(); while (itSpalten.hasNext()) { Integer spalte = itSpalten.next(); CodeSystemConceptTranslation translation = new CodeSystemConceptTranslation(); translation.setLanguageCd(headerTranslations.get(spalte)); translation.setTerm(csv.get(spalte)); csc.getCodeSystemConceptTranslations().add(translation); logger.debug("Translation hinzufgen: " + translation.getLanguageCd() + "," + translation.getTerm()); } } if (csc.getCode().length() == 0) { if (csc.getTerm().length() > 0) { if (csc.getTerm().length() > 98) csc.setCode(csc.getTerm().substring(0, 98)); else csc.setCode(csc.getTerm()); } } else if (csc.getCode().length() > 98) { csc.setCode(csc.getCode().substring(0, 98)); } if (csc.getCode().length() > 0) { // Entity-Version erstellen CodeSystemEntityVersion csev = new CodeSystemEntityVersion(); csev.setCodeSystemConcepts(new HashSet<CodeSystemConcept>()); csev.getCodeSystemConcepts().add(csc); csev.setStatusVisibility(1); csev.setIsLeaf(true); // Entity-Version dem Request hinzufgen request.getCodeSystemEntity().getCodeSystemEntityVersions().add(csev); // Dienst aufrufen (Konzept einfgen) CreateConcept cc = new CreateConcept(); CreateConceptResponseType response = cc.CreateConcept(request, hb_session, loginInfoType); if (response.getReturnInfos().getStatus() == ReturnType.Status.OK) { RelationMapType newMapEntry = new RelationMapType( response.getCodeSystemEntity().getId(), response.getCodeSystemEntity().getCurrentVersionId()); relationMap.put(csc.getCode(), newMapEntry); StaticStatus.importCount++; count++; if (count % 100 == 0) { hb_session.flush(); hb_session.clear(); } if (requestAssociation != null) { // Beziehung ebenfalls abspeichern requestAssociation.getCodeSystemEntityVersionAssociation() .setCodeSystemEntityVersionByCodeSystemEntityVersionId2( new CodeSystemEntityVersion()); requestAssociation.getCodeSystemEntityVersionAssociation() .getCodeSystemEntityVersionByCodeSystemEntityVersionId2() .setVersionId(response.getCodeSystemEntity().getCurrentVersionId()); // Dienst aufrufen (Beziehung erstellen) CreateConceptAssociation cca = new CreateConceptAssociation(); CreateConceptAssociationResponseType responseAssociation = cca .CreateConceptAssociation(requestAssociation, hb_session, loginInfoType); if (responseAssociation.getReturnInfos().getStatus() == ReturnType.Status.OK) { } else logger.debug("Beziehung nicht gespeichert: " + responseAssociation.getReturnInfos().getMessage()); } if (headerMetadata != null && headerMetadata.size() > 0) { // Metadaten einfgen for (Integer spalte : headerMetadata.keySet()) { String mdValue = csv.get(spalte); if (mdValue != null && mdValue.length() > 0) { CodeSystemMetadataValue csmv = new CodeSystemMetadataValue(mdValue); csmv.setCodeSystemEntityVersion(new CodeSystemEntityVersion()); //csmv.getCodeSystemEntityVersion().setVersionId(response.getCodeSystemEntity().getId()); csmv.getCodeSystemEntityVersion() .setVersionId(response.getCodeSystemEntity().getCurrentVersionId()); csmv.setMetadataParameter(new MetadataParameter()); csmv.getMetadataParameter() .setId(headerMetadataIDs.get(headerMetadata.get(spalte))); logger.debug("Metadaten einfgen, MP-ID " + csmv.getMetadataParameter().getId() + ", CSEV-ID " + csmv.getCodeSystemEntityVersion().getVersionId() + ", Wert: " + csmv.getParameterValue()); hb_session.save(csmv); } } } } else countFehler++; } else { countFehler++; logger.debug("Term ist nicht gegeben"); } } if (count == 0) { tx.rollback(); reponse.getReturnInfos().setMessage("Keine Konzepte importiert."); } else { if (StaticStatus.cancel) { tx.rollback(); reponse.getReturnInfos().setMessage("Import abgebrochen."); } else { tx.commit(); countImported = count; reponse.getReturnInfos().setMessage("Import abgeschlossen. " + count + " Konzept(e) importiert, " + countFehler + " Fehler"); } } } catch (Exception ex) { //ex.printStackTrace(); LoggingOutput.outputException(ex, this); //logger.error(ex.getMessage()); s = "Fehler beim Import einer CSV-Datei: " + ex.getLocalizedMessage(); try { tx.rollback(); logger.info("[ImportCSV.java] Rollback durchgefhrt!"); } catch (Exception exRollback) { logger.info(exRollback.getMessage()); logger.info("[ImportCSV.java] Rollback fehlgeschlagen!"); } } finally { // Session schlieen hb_session.close(); } } catch (Exception ex) { //java.util.logging.Logger.getLogger(ImportCodeSystem.class.getName()).log(Level.SEVERE, null, ex); s = "Fehler beim Import: " + ex.getLocalizedMessage(); logger.error(s); //ex.printStackTrace(); } return s; }
From source file:de.fhdo.terminologie.ws.administration._import.ImportLOINC.java
License:Apache License
public String importLOINC_Associations(ImportCodeSystemResponseType response) { StaticStatus.importCount = 0;/*from w ww. ja va 2s.c o m*/ StaticStatus.importTotal = 0; String s = ""; if (codesMap == null) codesMap = new HashMap<String, Long>(); int count = 0, countFehler = 0; CsvReader csv; try { logger.debug("codesMap-Size: " + codesMap.size()); byte[] bytes = parameter.getImportInfos().getFilecontent(); logger.debug("wandle zu InputStream um..."); InputStream is = new ByteArrayInputStream(bytes); csv = new CsvReader(is, Charset.forName("ISO-8859-1")); csv.setDelimiter('\t'); csv.setUseTextQualifier(true); csv.readHeaders(); logger.debug("Anzahl Header: " + csv.getHeaderCount()); for (int i = 0; i < csv.getHeaderCount(); ++i) logger.debug("Header: " + csv.getHeader(i)); // Hibernate-Block, Session ffnen org.hibernate.Session hb_session = HibernateUtil.getSessionFactory().openSession(); org.hibernate.Transaction tx = hb_session.beginTransaction(); try // try-catch-Block zum Abfangen von Hibernate-Fehlern { if (parameter.getCodeSystem() == null || parameter.getCodeSystem().getId() == 0 || parameter.getCodeSystem().getCodeSystemVersions() == null || parameter.getCodeSystem().getCodeSystemVersions().size() == 0) { // Fehlermeldung tx.rollback(); HibernateUtil.getSessionFactory().close(); return "Kein Codesystem angegeben!"; } // Assoziationen lesen associationMap = new HashMap<String, AssociationType>(); List<AssociationType> associationList = hb_session.createQuery("from AssociationType").list(); for (int i = 0; i < associationList.size(); ++i) { associationMap.put(associationList.get(i).getForwardName(), associationList.get(i)); } logger.debug("Starte Import..."); int countEvery = 0; // Daten laden while (csv.readRecord()) { //logger.warn(new Date().getTime() + ", Lese Index " + StaticStatus.importCount); countEvery++; CreateConceptAssociationRequestType request = new CreateConceptAssociationRequestType(); request.setLoginToken(parameter.getLoginToken()); request.setCodeSystemEntityVersionAssociation(new CodeSystemEntityVersionAssociation()); String code1 = csv.get("LOINC"); String code2 = csv.get("MAP_TO"); String comment = csv.get("COMMENT"); //logger.warn(new Date().getTime() + ", CSV gelesen"); //logger.warn("Code1: " + code1 + ", Code2: " + code2 + ", comment: " + comment); // CSV-ID fr LOINC-Code lesen long csev_id1 = getCSEV_VersionIdFromCode(code1); long csev_id2 = getCSEV_VersionIdFromCode(code2); //logger.warn(new Date().getTime() + ", Codes gelesen"); if (csev_id1 > 0 && csev_id2 > 0) { //logger.warn(new Date().getTime() + ", Fge Assoziation ein..."); // IDs setzen request.getCodeSystemEntityVersionAssociation() .setCodeSystemEntityVersionByCodeSystemEntityVersionId1( new CodeSystemEntityVersion()); request.getCodeSystemEntityVersionAssociation() .setCodeSystemEntityVersionByCodeSystemEntityVersionId2( new CodeSystemEntityVersion()); request.getCodeSystemEntityVersionAssociation() .getCodeSystemEntityVersionByCodeSystemEntityVersionId1().setVersionId(csev_id1); request.getCodeSystemEntityVersionAssociation() .getCodeSystemEntityVersionByCodeSystemEntityVersionId2().setVersionId(csev_id2); request.getCodeSystemEntityVersionAssociation().setAssociationKind(1); // ontologisch request.getCodeSystemEntityVersionAssociation().setLeftId(csev_id1); request.getCodeSystemEntityVersionAssociation().setAssociationType(new AssociationType()); // Assoziationstyp erhalten //logger.warn(new Date().getTime() + ", Assoziationstyp erhalten..."); if (comment != null && comment.length() > 0 && associationMap.containsKey(comment)) { request.getCodeSystemEntityVersionAssociation().getAssociationType() .setCodeSystemEntityVersionId( associationMap.get(comment).getCodeSystemEntityVersionId()); } else { if (comment != null && comment.length() > 0) { // Neuen Beziehungstyp einfgen CreateConceptAssociationTypeRequestType requestAssociation = new CreateConceptAssociationTypeRequestType(); requestAssociation.setLoginToken(parameter.getLoginToken()); requestAssociation.setCodeSystem(parameter.getCodeSystem()); requestAssociation.setCodeSystemEntity(new CodeSystemEntity()); requestAssociation.getCodeSystemEntity() .setCodeSystemEntityVersions(new HashSet<CodeSystemEntityVersion>()); AssociationType at = new AssociationType(); if (comment.length() > 48) at.setForwardName(comment.substring(0, 48)); else at.setForwardName(comment); at.setReverseName(""); CodeSystemEntityVersion csev = new CodeSystemEntityVersion(); csev.setAssociationTypes(new HashSet<AssociationType>()); csev.getAssociationTypes().add(at); requestAssociation.getCodeSystemEntity().getCodeSystemEntityVersions().add(csev); CreateConceptAssociationType ccat = new CreateConceptAssociationType(); CreateConceptAssociationTypeResponseType responseAssociation = ccat .CreateConceptAssociationType(requestAssociation, hb_session, loginInfoType); if (responseAssociation.getReturnInfos().getStatus() == ReturnType.Status.OK) { CodeSystemEntityVersion csev_result = (CodeSystemEntityVersion) responseAssociation .getCodeSystemEntity().getCodeSystemEntityVersions().toArray()[0]; request.getCodeSystemEntityVersionAssociation().getAssociationType() .setCodeSystemEntityVersionId(csev_result.getVersionId()); } else request.getCodeSystemEntityVersionAssociation().getAssociationType() .setCodeSystemEntityVersionId(5l); // TODO gehrt zu //logger.warn(new Date().getTime() + ", Neue Beziehung eingefgt"); } else { // Standard-Beziehung nehmen request.getCodeSystemEntityVersionAssociation().getAssociationType() .setCodeSystemEntityVersionId(5l); // TODO gehrt zu } } //logger.warn(new Date().getTime() + ", Assoziationstyp bestimmt"); StaticStatus.importCount++; if (StaticStatus.importCount % 100 == 0) logger.debug("Lese Datensatz " + StaticStatus.importCount + ", count: " + count); // Beziehung speichern // Dienst aufrufen (Konzept einfgen) //logger.warn(new Date().getTime() + ", Assoziation in DB einfgen..."); CreateConceptAssociation cca = new CreateConceptAssociation(); CreateConceptAssociationResponseType responseCCA = cca.CreateConceptAssociation(request, hb_session, loginInfoType); if (responseCCA.getReturnInfos().getStatus() == ReturnType.Status.OK) { count++; if (StaticStatus.importCount % 100 == 0) logger.debug( "Neue Beziehung hinzugefgt, ID1: " + csev_id1 + ", ID2: " + csev_id2); } else countFehler++; //logger.warn(new Date().getTime() + ", Assoziation in DB eingefgt"); } else { countFehler++; logger.debug("Term ist nicht angegeben"); } //logger.warn(new Date().getTime() + ", vor Speicherbereinigung"); //Mimimum acceptable free memory you think your app needs //long minRunningMemory = (1024 * 1024); //Runtime runtime = Runtime.getRuntime(); if (countEvery % 500 == 0) { //logger.debug("FreeMemory: " + runtime.freeMemory()); // wichtig, sonst kommt es bei greren Dateien zum Java-Heapspace-Fehler hb_session.flush(); hb_session.clear(); if (countEvery % 1000 == 0) { // sicherheitshalber aufrufen System.gc(); } } //logger.warn(new Date().getTime() + ", nach Speicherbereinigung"); //logger.warn(new Date().getTime() + ", Lese nchsten Record"); csv.skipLine(); if (countFehler > 10) break; } if (count == 0 || countFehler > 10) { tx.rollback(); response.getReturnInfos().setMessage( "Keine Beziehungen importiert. Mglicherweise ist die Fehleranzahl zu hoch. Anzahl Fehler: " + countFehler); } else { logger.debug("Import abgeschlossen, speicher Ergebnisse in DB (commit): " + count); tx.commit(); countImported = count; response.getReturnInfos().setMessage("Import abgeschlossen. " + count + " Beziehung(en) importiert, " + countFehler + " Fehler"); } } catch (Exception ex) { ex.printStackTrace(); logger.error("Fehler beim Import der LOINC-Association-Datei: " + ex.getMessage()); s = "Fehler beim Import der LOINC-Association-Datei: " + ex.getLocalizedMessage(); try { tx.rollback(); logger.info("[ImportLOINC.java] Rollback durchgefhrt!"); } catch (Exception exRollback) { logger.info(exRollback.getMessage()); logger.info("[ImportLOINC.java] Rollback fehlgeschlagen!"); } } finally { // Session schlieen HibernateUtil.getSessionFactory().close(); } } catch (Exception ex) { //java.util.logging.Logger.getLogger(ImportCodeSystem.class.getName()).log(Level.SEVERE, null, ex); s = "Fehler beim LOINC-Association-Import: " + ex.getLocalizedMessage(); ex.printStackTrace(); } associationMap.clear(); associationMap = null; codesMap.clear(); codesMap = null; logger.debug("ImportLOINC-Association - fertig"); return s; }
From source file:de.fhdo.terminologie.ws.administration._import.ImportLOINC.java
License:Apache License
public String importLOINC(ImportCodeSystemResponseType response) { StaticStatus.importCount = 0;//from www. j a va 2 s. com StaticStatus.importTotal = 0; String s = ""; int count = 0, countFehler = 0; codesMap = new HashMap<String, Long>(); CsvReader csv; try { byte[] bytes = parameter.getImportInfos().getFilecontent(); logger.debug("wandle zu InputStream um..."); InputStream is = new ByteArrayInputStream(bytes); csv = new CsvReader(is, Charset.forName("ISO-8859-1")); //csv = new CsvReader("C:\\Temp\\LOINCDB.TXT"); //csv.setDelimiter('\u0009'); // oder '\t' csv.setDelimiter('\t'); csv.setUseTextQualifier(true); csv.readHeaders(); logger.debug("Anzahl Header: " + csv.getHeaderCount()); // Hibernate-Block, Session ffnen org.hibernate.Session hb_session = HibernateUtil.getSessionFactory().openSession(); org.hibernate.Transaction tx = hb_session.beginTransaction(); try // try-catch-Block zum Abfangen von Hibernate-Fehlern { if (ImportCreateCodeSystem.createCodeSystem(hb_session, parameter, response) == false) { // Fehlermeldung tx.rollback(); return "CodeSystem konnte nicht erstellt werden!"; } // Metadaten-Parameter lesen metadataParameterMap = new HashMap<String, MetadataParameter>(); List<MetadataParameter> mpList = hb_session.createQuery("from MetadataParameter").list(); for (int i = 0; i < mpList.size(); ++i) { metadataParameterMap.put(mpList.get(i).getParamName(), mpList.get(i)); } logger.debug("Starte Import..."); // Request hier schon zusammenbauen (muss nicht tausendfach in der Schleife gemacht werden) //int countIndex = 0; // Daten laden // berlegen, was alles in der Schleife erzeugt wird (Speicherproblematik) while (csv.readRecord()) { CreateConceptRequestType request = new CreateConceptRequestType(); request.setLoginToken(parameter.getLoginToken()); request.setCodeSystem(parameter.getCodeSystem()); request.setCodeSystemEntity(new CodeSystemEntity()); request.getCodeSystemEntity() .setCodeSystemEntityVersions(new HashSet<CodeSystemEntityVersion>()); CodeSystemConcept csc = new CodeSystemConcept(); csc.setIsPreferred(true); CodeSystemEntityVersion csev = new CodeSystemEntityVersion(); csev.setCodeSystemConcepts(new HashSet<CodeSystemConcept>()); csev.setIsLeaf(true); CodeSystemConceptTranslation translation = new CodeSystemConceptTranslation(); translation.setLanguageCd(Definitions.LANGUAGE_ENGLISH_CD); CreateConcept cc = new CreateConcept(); StaticStatus.importCount++; if (StaticStatus.importCount % 200 == 0) logger.debug("Lese Datensatz " + StaticStatus.importCount + ", count: " + count); //request.setCodeSystemEntity(new CodeSystemEntity()); //request.getCodeSystemEntity().setCodeSystemEntityVersions(new HashSet<CodeSystemEntityVersion>()); csc.setCode(csv.get("LOINC_NUM")); csc.setTerm(csv.get("COMPONENT")); csc.setTermAbbrevation(csv.get("SHORTNAME")); // Sprachen prfen (Englisch-bersetzung hinzufgen) csc.getCodeSystemConceptTranslations().clear(); String translationEnglish = csv.get("LONG_COMMON_NAME"); if (translationEnglish != null && translationEnglish.length() > 0) { //csc.setCodeSystemConceptTranslations(new HashSet<CodeSystemConceptTranslation>()); //CodeSystemConceptTranslation translation = new CodeSystemConceptTranslation(); //translation.setLanguageId(Definitions.LANGUAGE_ENGLISH_ID); translation.setTerm(translationEnglish); csc.getCodeSystemConceptTranslations().add(translation); } // Daten anpassen if (csc.getCode().length() > 98) { csc.setCode(csc.getCode().substring(0, 98)); } // Entity-Version erstellen if (csev.getCodeSystemConcepts() == null) csev.setCodeSystemConcepts(new HashSet<CodeSystemConcept>()); else csev.getCodeSystemConcepts().clear(); csev.getCodeSystemConcepts().add(csc); csev.setEffectiveDate(parseDate(csv.get("DATE_LAST_CHANGED"))); csev.setStatusWorkflow(getLoincStatus(csv.get("STATUS"))); csev.setStatusVisibility(1); csev.setStatusVisibilityDate(new Date()); csev.setStatusWorkflowDate(new Date()); // Konzept speichern if (csc.getCode().length() > 0) { // Entity-Version dem Request hinzufgen request.getCodeSystemEntity().getCodeSystemEntityVersions().clear(); request.getCodeSystemEntity().getCodeSystemEntityVersions().add(csev); // Dienst aufrufen (Konzept einfgen) CreateConceptResponseType responseCC = cc.CreateConcept(request, hb_session, loginInfoType); if (responseCC.getReturnInfos().getStatus() == ReturnType.Status.OK) { count++; if (responseCC.getCodeSystemEntity().getCurrentVersionId() > 0) codesMap.put(csc.getCode(), responseCC.getCodeSystemEntity().getCurrentVersionId()); // Metadaten zu diesem Konzept speichern int mdCount = 0; CodeSystemEntityVersion csev_result = (CodeSystemEntityVersion) responseCC .getCodeSystemEntity().getCodeSystemEntityVersions().toArray()[0]; mdCount = addMetadataToConcept(csv, csev_result.getVersionId(), hb_session); if (StaticStatus.importCount % 200 == 0) logger.debug("Neues Konzept hinzugefgt: " + csc.getCode() + ", Anz. Metadaten: " + mdCount); } else countFehler++; } else { countFehler++; logger.debug("Term ist nicht angegeben"); } //Mimimum acceptable free memory you think your app needs //long minRunningMemory = (1024 * 1024); Runtime runtime = Runtime.getRuntime(); if (StaticStatus.importCount % 200 == 0) { logger.debug("FreeMemory: " + runtime.freeMemory()); if (StaticStatus.importCount % 1000 == 0) { // wichtig, sonst kommt es bei greren Dateien zum Java-Heapspace-Fehler hb_session.flush(); hb_session.clear(); } if (StaticStatus.importCount % 10000 == 0) { // sicherheitshalber aufrufen System.gc(); } } } if (count == 0) { tx.rollback(); response.getReturnInfos().setMessage("Keine Konzepte importiert."); } else { logger.debug("Import abgeschlossen, speicher Ergebnisse in DB (commit): " + count); tx.commit(); countImported = count; response.getReturnInfos().setMessage("Import abgeschlossen. " + count + " Konzept(e) importiert, " + countFehler + " Fehler"); } } catch (Exception ex) { ex.printStackTrace(); logger.error("Fehler beim Import der LOINC-Datei: " + ex.getMessage()); s = "Fehler beim Import der LOINC-Datei: " + ex.getLocalizedMessage(); try { tx.rollback(); logger.info("[ImportLOINC.java] Rollback durchgefhrt!"); } catch (Exception exRollback) { logger.info(exRollback.getMessage()); logger.info("[ImportLOINC.java] Rollback fehlgeschlagen!"); } } finally { // Session schlieen HibernateUtil.getSessionFactory().close(); } } catch (Exception ex) { //java.util.logging.Logger.getLogger(ImportCodeSystem.class.getName()).log(Level.SEVERE, null, ex); s = "Fehler beim LOINC-Import: " + ex.getLocalizedMessage(); ex.printStackTrace(); } StaticStatus.importCount = 0; StaticStatus.importTotal = 0; logger.debug("ImportLOINC - fertig"); return s; }
From source file:de.fhdo.terminologie.ws.administration._import.ImportLOINC_ELGA.java
License:Apache License
public String importLOINC_Associations(ImportCodeSystemResponseType response) { StaticStatus.importCount = 0;/*from w w w . jav a 2 s. co m*/ StaticStatus.importTotal = 0; String s = ""; if (codesMap == null) codesMap = new HashMap<String, Long>(); int count = 0, countFehler = 0; CsvReader csv; try { logger.debug("codesMap-Size: " + codesMap.size()); byte[] bytes = parameter.getImportInfos().getFilecontent(); logger.debug("wandle zu InputStream um..."); InputStream is = new ByteArrayInputStream(bytes); csv = new CsvReader(is, Charset.forName("ISO-8859-1")); csv.setDelimiter('\t'); csv.setUseTextQualifier(true); csv.readHeaders(); logger.debug("Anzahl Header: " + csv.getHeaderCount()); for (int i = 0; i < csv.getHeaderCount(); ++i) logger.debug("Header: " + csv.getHeader(i)); // Hibernate-Block, Session ffnen org.hibernate.Session hb_session = HibernateUtil.getSessionFactory().openSession(); hb_session.getTransaction().begin(); try // try-catch-Block zum Abfangen von Hibernate-Fehlern { if (parameter.getCodeSystem() == null || parameter.getCodeSystem().getId() == 0 || parameter.getCodeSystem().getCodeSystemVersions() == null || parameter.getCodeSystem().getCodeSystemVersions().size() == 0) { // Fehlermeldung hb_session.getTransaction().rollback(); hb_session.close(); return "Kein Codesystem angegeben!"; } csId = parameter.getCodeSystem().getId(); csvId = parameter.getCodeSystem().getCodeSystemVersions().iterator().next().getVersionId(); // Assoziationen lesen associationMap = new HashMap<String, AssociationType>(); List<AssociationType> associationList = hb_session.createQuery("from AssociationType").list(); for (int i = 0; i < associationList.size(); ++i) { associationMap.put(associationList.get(i).getForwardName(), associationList.get(i)); } logger.debug("Starte Import..."); int countEvery = 0; // Daten laden while (csv.readRecord()) { //logger.warn(new Date().getTime() + ", Lese Index " + StaticStatus.importCount); countEvery++; CreateConceptAssociationRequestType request = new CreateConceptAssociationRequestType(); request.setLoginToken(parameter.getLoginToken()); request.setCodeSystemEntityVersionAssociation(new CodeSystemEntityVersionAssociation()); String code1 = csv.get("LOINC"); String code2 = csv.get("MAP_TO"); String comment = csv.get("COMMENT"); //logger.warn(new Date().getTime() + ", CSV gelesen"); //logger.warn("Code1: " + code1 + ", Code2: " + code2 + ", comment: " + comment); // CSV-ID fr LOINC-Code lesen long csev_id1 = getCSEV_VersionIdFromCode(code1); long csev_id2 = getCSEV_VersionIdFromCode(code2); //logger.warn(new Date().getTime() + ", Codes gelesen"); if (csev_id1 > 0 && csev_id2 > 0) { //logger.warn(new Date().getTime() + ", Fge Assoziation ein..."); // IDs setzen request.getCodeSystemEntityVersionAssociation() .setCodeSystemEntityVersionByCodeSystemEntityVersionId1( new CodeSystemEntityVersion()); request.getCodeSystemEntityVersionAssociation() .setCodeSystemEntityVersionByCodeSystemEntityVersionId2( new CodeSystemEntityVersion()); request.getCodeSystemEntityVersionAssociation() .getCodeSystemEntityVersionByCodeSystemEntityVersionId1().setVersionId(csev_id1); request.getCodeSystemEntityVersionAssociation() .getCodeSystemEntityVersionByCodeSystemEntityVersionId2().setVersionId(csev_id2); request.getCodeSystemEntityVersionAssociation().setAssociationKind(1); // ontologisch request.getCodeSystemEntityVersionAssociation().setLeftId(csev_id1); request.getCodeSystemEntityVersionAssociation().setAssociationType(new AssociationType()); // Assoziationstyp erhalten //logger.warn(new Date().getTime() + ", Assoziationstyp erhalten..."); if (comment != null && comment.length() > 0 && associationMap.containsKey(comment)) { request.getCodeSystemEntityVersionAssociation().getAssociationType() .setCodeSystemEntityVersionId( associationMap.get(comment).getCodeSystemEntityVersionId()); } else { if (comment != null && comment.length() > 0) { // Neuen Beziehungstyp einfgen CreateConceptAssociationTypeRequestType requestAssociation = new CreateConceptAssociationTypeRequestType(); requestAssociation.setLoginToken(parameter.getLoginToken()); requestAssociation.setCodeSystem(parameter.getCodeSystem()); requestAssociation.setCodeSystemEntity(new CodeSystemEntity()); requestAssociation.getCodeSystemEntity() .setCodeSystemEntityVersions(new HashSet<CodeSystemEntityVersion>()); AssociationType at = new AssociationType(); if (comment.length() > 48) at.setForwardName(comment.substring(0, 48)); else at.setForwardName(comment); at.setReverseName(""); CodeSystemEntityVersion csev = new CodeSystemEntityVersion(); csev.setAssociationTypes(new HashSet<AssociationType>()); csev.getAssociationTypes().add(at); requestAssociation.getCodeSystemEntity().getCodeSystemEntityVersions().add(csev); CreateConceptAssociationType ccat = new CreateConceptAssociationType(); CreateConceptAssociationTypeResponseType responseAssociation = ccat .CreateConceptAssociationType(requestAssociation, hb_session, loginInfoType); if (responseAssociation.getReturnInfos().getStatus() == ReturnType.Status.OK) { CodeSystemEntityVersion csev_result = (CodeSystemEntityVersion) responseAssociation .getCodeSystemEntity().getCodeSystemEntityVersions().toArray()[0]; request.getCodeSystemEntityVersionAssociation().getAssociationType() .setCodeSystemEntityVersionId(csev_result.getVersionId()); } else request.getCodeSystemEntityVersionAssociation().getAssociationType() .setCodeSystemEntityVersionId(5L); // gehrt zu //logger.warn(new Date().getTime() + ", Neue Beziehung eingefgt"); } else { // Standard-Beziehung nehmen request.getCodeSystemEntityVersionAssociation().getAssociationType() .setCodeSystemEntityVersionId(5L); // gehrt zu } } //logger.warn(new Date().getTime() + ", Assoziationstyp bestimmt"); StaticStatus.importCount++; if (StaticStatus.importCount % 100 == 0) logger.debug("Lese Datensatz " + StaticStatus.importCount + ", count: " + count); // Beziehung speichern // Dienst aufrufen (Konzept einfgen) //logger.warn(new Date().getTime() + ", Assoziation in DB einfgen..."); CreateConceptAssociation cca = new CreateConceptAssociation(); CreateConceptAssociationResponseType responseCCA = cca.CreateConceptAssociation(request, hb_session, loginInfoType); if (responseCCA.getReturnInfos().getStatus() == ReturnType.Status.OK) { count++; if (StaticStatus.importCount % 100 == 0) logger.debug( "Neue Beziehung hinzugefgt, ID1: " + csev_id1 + ", ID2: " + csev_id2); } else countFehler++; //logger.warn(new Date().getTime() + ", Assoziation in DB eingefgt"); } else { countFehler++; logger.debug("Term ist nicht angegeben"); } //logger.warn(new Date().getTime() + ", vor Speicherbereinigung"); //Mimimum acceptable free memory you think your app needs //long minRunningMemory = (1024 * 1024); //Runtime runtime = Runtime.getRuntime(); if (countEvery % 500 == 0) { //logger.debug("FreeMemory: " + runtime.freeMemory()); // wichtig, sonst kommt es bei greren Dateien zum Java-Heapspace-Fehler hb_session.flush(); hb_session.clear(); if (countEvery % 1000 == 0) { // sicherheitshalber aufrufen System.gc(); } } //logger.warn(new Date().getTime() + ", nach Speicherbereinigung"); //logger.warn(new Date().getTime() + ", Lese nchsten Record"); csv.skipLine(); if (countFehler > 10) break; } if (count == 0 || countFehler > 10) { hb_session.getTransaction().rollback(); resultStr = DeleteTermHelperWS.deleteCS_CSV(hb_session, onlyCSV, csId, csvId); response.getReturnInfos().setMessage( "Keine Beziehungen importiert. Mglicherweise ist die Fehleranzahl zu hoch. Anzahl Fehler: " + countFehler); } else { logger.debug("Import abgeschlossen, speicher Ergebnisse in DB (commit): " + count); hb_session.getTransaction().commit(); countImported = count; response.getReturnInfos().setMessage("Import abgeschlossen. " + count + " Beziehung(en) importiert, " + countFehler + " Fehler"); } } catch (Exception ex) { ex.printStackTrace(); logger.error("Fehler beim Import der LOINC-Association-Datei: " + ex.getMessage()); s = "Fehler beim Import der LOINC-Association-Datei: " + ex.getLocalizedMessage(); try { hb_session.getTransaction().rollback(); resultStr = DeleteTermHelperWS.deleteCS_CSV(hb_session, onlyCSV, csId, csvId); logger.info("[ImportLOINC.java] Rollback durchgefuehrt!"); } catch (Exception exRollback) { logger.info(exRollback.getMessage()); logger.info("[ImportLOINC.java] Rollback fehlgeschlagen!"); } } finally { // Session schlieen hb_session.close(); } } catch (Exception ex) { //java.util.logging.Logger.getLogger(ImportCodeSystem.class.getName()).log(Level.SEVERE, null, ex); s = "Fehler beim LOINC-Association-Import: " + ex.getLocalizedMessage(); ex.printStackTrace(); } associationMap.clear(); associationMap = null; codesMap.clear(); codesMap = null; logger.debug("ImportLOINC-Association - fertig"); return s; }
From source file:de.fhdo.terminologie.ws.administration._import.ImportLOINC_ELGA.java
License:Apache License
public String importLOINC(ImportCodeSystemResponseType response) { //Store csv in DB /*Configuration******************************************************************************************************/ /**/ String path = System.getProperty("catalina.base") + "/conf/data/actualLOINC.csv"; // test /*Productive_AT_PU***************************************************************************************************/ /**/ //String path = System.getProperty("catalina.base") + "/conf/data/actualLOINC.csv"; // public test /**/ //String path = "/data0/web/tomcat_pub/conf/data/actualLOINC.csv"; // public prod /**/ //String path = "/data0/web/tomcat_col/conf/data/actualLOINC.csv"; // kollab prod /**************************************************************************************************************************/ /**/ //String path = "/data0/web/tomcat_term1/conf/data/actualLOINC.csv"; // testSystem BRZ /**************************************************************************************************************************/ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); byte[] bytes = parameter.getImportInfos().getFilecontent(); if (!parameter.getImportInfos().getOrder()) { StaticStatus.importCount = 0;/*from w w w . j av a 2 s. com*/ StaticStatus.importTotal = 0; ArrayList<String> errList = new ArrayList<String>(); String s = ""; int count = 0, countFehler = 0; codesMap = new HashMap<String, Long>(); CsvReader csv; try { logger.debug("Wandle zu InputStream um..."); InputStream is = new ByteArrayInputStream(bytes); csv = new CsvReader(is, Charset.forName("UTF-8")); csv.setDelimiter(','); csv.setTextQualifier('"'); csv.setUseTextQualifier(true); csv.readHeaders(); logger.debug("Anzahl Header: " + csv.getHeaderCount()); // Hibernate-Block, Session ffnen org.hibernate.Session hb_session = HibernateUtil.getSessionFactory().openSession(); hb_session.getTransaction().begin(); try // try-catch-Block zum Abfangen von Hibernate-Fehlern { CodeSystem cs_db = null; //check if cs exists if yes => new version if not if (parameter.getCodeSystem().getId() != null) cs_db = (CodeSystem) hb_session.get(CodeSystem.class, parameter.getCodeSystem().getId()); CodeSystemVersion csv2 = new CodeSystemVersion(); csv2.setCodeSystem(cs_db); Date d = new Date(); csv2.setInsertTimestamp(d); csv2.setName(parameter.getCodeSystem().getCodeSystemVersions().iterator().next().getName()); csv2.setPreviousVersionId(cs_db.getCurrentVersionId()); csv2.setStatus(1); csv2.setStatusDate(d); csv2.setPreferredLanguageCd("de"); csv2.setUnderLicence(false); csv2.setValidityRange(236l); csv2.setOid("2.16.840.1.113883.6.1"); hb_session.save(csv2); cs_db.setCurrentVersionId(csv2.getVersionId()); cs_db.getCodeSystemVersions().add(csv2); hb_session.update(cs_db); cs_db.getCodeSystemVersions().clear(); cs_db.getCodeSystemVersions().add(csv2); parameter.setCodeSystem(cs_db); // Metadaten-Parameter lesen metadataParameterMap = new HashMap<String, MetadataParameter>(); List<MetadataParameter> mpList = hb_session.createQuery("from MetadataParameter").list(); for (int i = 0; i < mpList.size(); ++i) { metadataParameterMap.put(mpList.get(i).getParamName(), mpList.get(i)); } logger.debug("Starte Import..."); while (csv.readRecord()) { CreateConceptRequestType request = new CreateConceptRequestType(); request.setLoginToken(parameter.getLoginToken()); request.setCodeSystem(parameter.getCodeSystem()); request.setCodeSystemEntity(new CodeSystemEntity()); request.getCodeSystemEntity() .setCodeSystemEntityVersions(new HashSet<CodeSystemEntityVersion>()); CodeSystemConcept csc = new CodeSystemConcept(); csc.setIsPreferred(true); CodeSystemEntityVersion csev = new CodeSystemEntityVersion(); csev.setCodeSystemConcepts(new HashSet<CodeSystemConcept>()); csev.setIsLeaf(true); CodeSystemVersionEntityMembership membership = new CodeSystemVersionEntityMembership(); membership.setIsMainClass(Boolean.TRUE); membership.setIsAxis(Boolean.FALSE); request.getCodeSystemEntity().setCodeSystemVersionEntityMemberships( new HashSet<CodeSystemVersionEntityMembership>()); request.getCodeSystemEntity().getCodeSystemVersionEntityMemberships().add(membership); CreateConcept cc = new CreateConcept(); StaticStatus.importCount++; if (StaticStatus.importCount % 200 == 0) logger.debug("Lese Datensatz " + StaticStatus.importCount + ", count: " + count); //request.setCodeSystemEntity(new CodeSystemEntity()); //request.getCodeSystemEntity().setCodeSystemEntityVersions(new HashSet<CodeSystemEntityVersion>()); csc.setCode(csv.get(LOINC_NUM)); csc.setTerm(csv.get("LONG_COMMON_NAME")); csc.setTermAbbrevation(csv.get("SHORTNAME")); csc.setDescription(csv.get("COMPONENT") + " | " + csv.get("PROPERTY") + " | " + csv.get("TIME_ASPCT") + " | " + csv.get("SYSTEM") + " | " + csv.get("SCALE_TYP") + " | " + csv.get("METHOD_TYP") + " | "); // Entity-Version erstellen if (csev.getCodeSystemConcepts() == null) csev.setCodeSystemConcepts(new HashSet<CodeSystemConcept>()); else csev.getCodeSystemConcepts().clear(); csev.getCodeSystemConcepts().add(csc); csev.setEffectiveDate(parseDate(csv.get("DATE_LAST_CHANGED"))); csev.setStatusVisibility(1); //Fix laut Mail vom 25.06.2014 13:48 // Konzept speichern if (csc.getCode().length() > 0) { // Entity-Version dem Request hinzufgen request.getCodeSystemEntity().getCodeSystemEntityVersions().clear(); request.getCodeSystemEntity().getCodeSystemEntityVersions().add(csev); // Dienst aufrufen (Konzept einfgen) CreateConceptResponseType responseCC = cc.CreateConcept(request, hb_session, loginInfoType); if (responseCC.getReturnInfos().getStatus() == ReturnType.Status.OK) { count++; if (responseCC.getCodeSystemEntity().getCurrentVersionId() > 0) codesMap.put(csc.getCode(), responseCC.getCodeSystemEntity().getCurrentVersionId()); // Metadaten zu diesem Konzept speichern int mdCount = 0; CodeSystemEntityVersion csev_result = (CodeSystemEntityVersion) responseCC .getCodeSystemEntity().getCodeSystemEntityVersions().toArray()[0]; mdCount = addMetadataToConcept(csv, csev_result.getVersionId(), hb_session, parameter.getCodeSystem().getId()); //System.out.println(count); } else { countFehler++; errList.add(String.valueOf(count)); } } else { countFehler++; errList.add(String.valueOf(count)); logger.debug("Term ist nicht angegeben"); } //Mimimum acceptable free memory you think your app needs //long minRunningMemory = (1024 * 1024); Runtime runtime = Runtime.getRuntime(); if (StaticStatus.importCount % 200 == 0) { logger.debug("FreeMemory: " + runtime.freeMemory()); if (StaticStatus.importCount % 1000 == 0) { // wichtig, sonst kommt es bei greren Dateien zum Java-Heapspace-Fehler hb_session.flush(); hb_session.clear(); } if (StaticStatus.importCount % 10000 == 0) { // sicherheitshalber aufrufen System.gc(); } } } for (String str : errList) { System.out.println("-----Zeile: " + str + "-----\n"); } if (count == 0) { hb_session.getTransaction().rollback(); response.getReturnInfos().setMessage("Keine Konzepte importiert."); } else { logger.debug("Import abgeschlossen, speicher Ergebnisse in DB (commit): " + count); hb_session.getTransaction().commit(); countImported = count; response.getReturnInfos().setMessage("Import abgeschlossen. " + count + " Konzept(e) importiert, " + countFehler + " Fehler"); } } catch (Exception ex) { ex.printStackTrace(); logger.error("Fehler beim Import der LOINC-Datei: " + ex.getMessage()); s = "Fehler beim Import der LOINC-Datei: " + ex.getLocalizedMessage(); try { hb_session.getTransaction().rollback(); logger.info("[ImportLOINC.java] Rollback durchgefuehrt!"); } catch (Exception exRollback) { logger.info(exRollback.getMessage()); logger.info("[ImportLOINC.java] Rollback fehlgeschlagen!"); } } finally { // Session schlieen hb_session.close(); } } catch (Exception ex) { //java.util.logging.Logger.getLogger(ImportCodeSystem.class.getName()).log(Level.SEVERE, null, ex); s = "Fehler beim LOINC-Import: " + ex.getLocalizedMessage(); ex.printStackTrace(); } StaticStatus.importCount = 0; StaticStatus.importTotal = 0; logger.debug("ImportLOINC - fertig"); //Store actual "Version" FileOutputStream fos; try { fos = new FileOutputStream(path); Writer out = new OutputStreamWriter(fos, "UTF8"); out.write(new String(bytes, "UTF-8")); out.close(); } catch (FileNotFoundException ex) { logger.error(ex); } catch (IOException ex) { logger.error(ex); } return s; } else { //Abgleich LOINC fr Tab-Separated LOINC File! System.out.println("LOINC Import-Update gestartet: " + sdf.format(new Date())); //Get previous Version and actual Version as CSV String s = ""; boolean err = false; StaticStatus.importCount = 0; StaticStatus.importTotal = 0; ArrayList<String> errList = new ArrayList<String>(); int count = 0, countFehler = 0, newCount = 0; codesMap = new HashMap<String, Long>(); CsvReader csvAct; CsvReader csvPrev; HashMap<String, String> prevLoinc = new HashMap<String, String>(); try { InputStream isAct = new ByteArrayInputStream(bytes); csvAct = new CsvReader(isAct, Charset.forName("UTF-8")); csvAct.setDelimiter(','); csvAct.setTextQualifier('"'); csvAct.setUseTextQualifier(true); File file = new File(path); FileInputStream fis = new FileInputStream(file); byte bytesPrev[] = new byte[(int) file.length()]; fis.read(bytesPrev); InputStream isPrev = new ByteArrayInputStream(bytesPrev); csvPrev = new CsvReader(isPrev, Charset.forName("UTF-8")); csvPrev.setDelimiter(','); csvPrev.setTextQualifier('"'); csvPrev.setUseTextQualifier(true); csvPrev.readHeaders(); //Prepare HashMap to compare while (csvPrev.readRecord()) { prevLoinc.put(csvPrev.get(LOINC_NUM), csvPrev.getRawRecord()); } csvPrev.close(); csvAct.readHeaders(); // Hibernate-Block, Session ffnen org.hibernate.Session hb_session = HibernateUtil.getSessionFactory().openSession(); hb_session.getTransaction().begin(); // Metadaten-Parameter lesen metadataParameterMap = new HashMap<String, MetadataParameter>(); List<MetadataParameter> mpList = hb_session .createQuery("from MetadataParameter mp join fetch mp.codeSystem cs where cs.name='LOINC'") .list(); for (int i = 0; i < mpList.size(); ++i) { metadataParameterMap.put(mpList.get(i).getParamName(), mpList.get(i)); } CodeSystem cs_db = null; //check if cs exists if yes => new version if not if (parameter.getCodeSystem().getId() != null) cs_db = (CodeSystem) hb_session.get(CodeSystem.class, parameter.getCodeSystem().getId()); parameter.setCodeSystem(cs_db); while (csvAct.readRecord()) { String actKey = csvAct.get(LOINC_NUM); StaticStatus.importCount++; if (prevLoinc.containsKey(actKey)) { //Vorhanden => Check for update String prevRaw = prevLoinc.get(actKey); String actRaw = csvAct.getRawRecord(); if (!prevRaw.equals(actRaw)) { //Something has changed //Get CSEV and all Metadata for update String hqlString = "select distinct csev from CodeSystemEntityVersion csev join fetch csev.codeSystemConcepts csc join fetch csev.codeSystemMetadataValues csmv join fetch csmv.metadataParameter mp join fetch mp.codeSystem cs"; hqlString += " where cs.id=:cs_id and csc.code=:code"; Query q = hb_session.createQuery(hqlString); q.setParameter("cs_id", parameter.getCodeSystem().getId()); q.setParameter("code", actKey); List<CodeSystemEntityVersion> csevList = q.list(); if (csevList != null && !csevList.isEmpty()) { CodeSystemEntityVersion csev_db = null; CodeSystemConcept csc_db = null; try { //Update CSEV csev_db = (CodeSystemEntityVersion) hb_session .load(CodeSystemEntityVersion.class, csevList.get(0).getVersionId()); csev_db.setEffectiveDate(parseDate(csvAct.get("DATE_LAST_CHANGED"))); csev_db.setStatusVisibility(1); hb_session.update(csev_db); //Update CSC csc_db = (CodeSystemConcept) hb_session.load(CodeSystemConcept.class, csevList.get(0).getCodeSystemConcepts().iterator().next() .getCodeSystemEntityVersionId()); csc_db.setCode(actKey); csc_db.setTerm(csvAct.get("LONG_COMMON_NAME")); csc_db.setTermAbbrevation(csvAct.get("SHORTNAME")); csc_db.setDescription(csvAct.get("COMPONENT") + " | " + csvAct.get("PROPERTY") + " | " + csvAct.get("TIME_ASPCT") + " | " + csvAct.get("SYSTEM") + " | " + csvAct.get("SCALE_TYP") + " | " + csvAct.get("METHOD_TYP") + " | "); hb_session.update(csc_db); //Update Metadata HashMap<String, CodeSystemMetadataValue> csmvList = new HashMap<String, CodeSystemMetadataValue>(); for (CodeSystemMetadataValue csmv : csevList.get(0) .getCodeSystemMetadataValues()) csmvList.put(csmv.getMetadataParameter().getParamName(), csmv); for (int i = 0; i < metadataFields.length; ++i) { String content = csvAct.get(metadataFields[i]); CodeSystemMetadataValue csmv = csmvList.get(metadataFields[i]); if (csmv != null) { //Update CodeSystemMetadataValue csmv_db = (CodeSystemMetadataValue) hb_session .load(CodeSystemMetadataValue.class, csmv.getId()); csmv_db.setParameterValue(content); hb_session.update(csmv_db); } else { //Noch nicht angelegt => Check if != "" if (content.length() > 0) { //Neues CSMV plus link auf MP CodeSystemMetadataValue mv = new CodeSystemMetadataValue(); mv.setParameterValue(content); mv.setCodeSystemEntityVersion(new CodeSystemEntityVersion()); mv.getCodeSystemEntityVersion() .setVersionId((Long) csev_db.getVersionId()); mv.setMetadataParameter(new MetadataParameter()); MetadataParameter mp = null; if (metadataParameterMap.containsKey(metadataFields[i])) mp = metadataParameterMap.get(metadataFields[i]); else { //Create metadata_parameter mp = new MetadataParameter(); mp.setParamName(metadataFields[i]); mp.setCodeSystem(new CodeSystem()); mp.getCodeSystem().setId(parameter.getCodeSystem().getId()); hb_session.save(mp); metadataParameterMap.put(metadataFields[i], mp); } mv.getMetadataParameter().setId(mp.getId()); hb_session.save(mv); } } } count++; System.out.println("LOINC Konzept(" + actKey + ") update durchgefuehrt: " + StaticStatus.importCount); } catch (Exception e) { countFehler++; errList.add(String.valueOf(newCount)); //logger.debug("Fehler im Update-Import Loinc: Vergleich zweier Eintrge fehlerhaft!"); System.out.println("LOINC Konzept(" + actKey + ") update durchgefuehrt FEHLER: " + StaticStatus.importCount); } } else { countFehler++; errList.add(String.valueOf(newCount)); //logger.debug("Code nicht gefunden!"); System.out.println("LOINC Konzept(" + actKey + ") update durchgefuehrt FEHLER: " + StaticStatus.importCount); } } else { //System.out.println("LOINC Konzept(" + actKey + ") update nicht noetig: " + countNr); } } else { //New entry! CreateConceptRequestType request = new CreateConceptRequestType(); request.setLoginToken(parameter.getLoginToken()); request.setCodeSystem(parameter.getCodeSystem()); request.setCodeSystemEntity(new CodeSystemEntity()); request.getCodeSystemEntity() .setCodeSystemEntityVersions(new HashSet<CodeSystemEntityVersion>()); CodeSystemConcept csc = new CodeSystemConcept(); csc.setIsPreferred(true); CodeSystemEntityVersion csev = new CodeSystemEntityVersion(); csev.setCodeSystemConcepts(new HashSet<CodeSystemConcept>()); csev.setIsLeaf(true); CodeSystemVersionEntityMembership membership = new CodeSystemVersionEntityMembership(); membership.setIsMainClass(Boolean.TRUE); membership.setIsAxis(Boolean.FALSE); request.getCodeSystemEntity().setCodeSystemVersionEntityMemberships( new HashSet<CodeSystemVersionEntityMembership>()); request.getCodeSystemEntity().getCodeSystemVersionEntityMemberships().add(membership); CreateConcept cc = new CreateConcept(); //request.setCodeSystemEntity(new CodeSystemEntity()); //request.getCodeSystemEntity().setCodeSystemEntityVersions(new HashSet<CodeSystemEntityVersion>()); csc.setCode(actKey); csc.setTerm(csvAct.get("LONG_COMMON_NAME")); csc.setTermAbbrevation(csvAct.get("SHORTNAME")); csc.setDescription(csvAct.get("COMPONENT") + " | " + csvAct.get("PROPERTY") + " | " + csvAct.get("TIME_ASPCT") + " | " + csvAct.get("SYSTEM") + " | " + csvAct.get("SCALE_TYP") + " | " + csvAct.get("METHOD_TYP") + " | "); // Entity-Version erstellen if (csev.getCodeSystemConcepts() == null) csev.setCodeSystemConcepts(new HashSet<CodeSystemConcept>()); else csev.getCodeSystemConcepts().clear(); csev.getCodeSystemConcepts().add(csc); csev.setEffectiveDate(parseDate(csvAct.get("DATE_LAST_CHANGED"))); csev.setStatusVisibility(1); // Konzept speichern if (csc.getCode().length() > 0) { // Entity-Version dem Request hinzufgen request.getCodeSystemEntity().getCodeSystemEntityVersions().clear(); request.getCodeSystemEntity().getCodeSystemEntityVersions().add(csev); // Dienst aufrufen (Konzept einfgen) CreateConceptResponseType responseCC = cc.CreateConcept(request, hb_session, loginInfoType); if (responseCC.getReturnInfos().getStatus() == ReturnType.Status.OK) { newCount++; if (responseCC.getCodeSystemEntity().getCurrentVersionId() > 0) codesMap.put(csc.getCode(), responseCC.getCodeSystemEntity().getCurrentVersionId()); // Metadaten zu diesem Konzept speichern int mdCount = 0; CodeSystemEntityVersion csev_result = (CodeSystemEntityVersion) responseCC .getCodeSystemEntity().getCodeSystemEntityVersions().toArray()[0]; mdCount = addMetadataToConcept(csvAct, csev_result.getVersionId(), hb_session, parameter.getCodeSystem().getId()); //System.out.println(count); System.out.println( "LOINC Konzept(" + actKey + ") neu erstellt: " + StaticStatus.importCount); } else { countFehler++; errList.add(String.valueOf(newCount)); System.out.println("LOINC Konzept(" + actKey + ") neu erstellt FEHLER: " + StaticStatus.importCount); } } else { countFehler++; errList.add(String.valueOf(newCount)); //logger.debug("Term ist nicht angegeben"); System.out.println("LOINC Konzept(" + actKey + ") neu erstellt FEHLER: " + StaticStatus.importCount); } } //Mimimum acceptable free memory you think your app needs //long minRunningMemory = (1024 * 1024); Runtime runtime = Runtime.getRuntime(); if (StaticStatus.importCount % 200 == 0) { logger.debug("FreeMemory: " + runtime.freeMemory()); if (StaticStatus.importCount % 1000 == 0) { // wichtig, sonst kommt es bei greren Dateien zum Java-Heapspace-Fehler hb_session.flush(); hb_session.clear(); } if (StaticStatus.importCount % 10000 == 0) { // sicherheitshalber aufrufen System.gc(); } } } csvAct.close(); logger.debug("Update-Import abgeschlossen, speicher Ergebnisse in DB (commit): " + count); if (countFehler == 0) hb_session.getTransaction().commit(); response.getReturnInfos() .setMessage("Update-Import abgeschlossen. Update bei " + count + " Konzept(en). " + newCount + " Konzepte wurden neu importiert. " + countFehler + " Fehler"); } catch (Exception ex) { s = "Fehler beim LOINC-Import - Update: " + ex.getLocalizedMessage(); err = true; } StaticStatus.importCount = 0; StaticStatus.importTotal = 0; logger.debug("ImportLOINC - Update - fertig"); System.out.println("ImportLOINC - Update - fertig: " + sdf.format(new Date())); //Store actual "Version" only if no error occured! if (countFehler == 0 && !err) { FileOutputStream fos; try { fos = new FileOutputStream(path); Writer out = new OutputStreamWriter(fos, "UTF8"); out.write(new String(bytes, "UTF-8")); out.close(); } catch (FileNotFoundException ex) { logger.error(ex); } catch (IOException ex) { logger.error(ex); } } return s; } }
From source file:de.fhdo.terminologie.ws.administration._import.ImportLOINC_V254.java
License:Apache License
public String importLOINC_Associations(ImportCodeSystemResponseType response) { StaticStatus.importCount = 0;/*from www . j a v a 2s . co m*/ StaticStatus.importTotal = 0; String s = ""; //if (codesMap == null) // codesMap = new HashMap<String, Long>(); int count = 0, countFehler = 0; CsvReader csv; try { //slogger.debug("codesMap-Size: " + codesMap.size()); byte[] bytes = parameter.getImportInfos().getFilecontent(); logger.debug("convert to InputStream..."); InputStream is = new ByteArrayInputStream(bytes); csv = new CsvReader(is, Charset.forName("ISO-8859-1")); csv.setDelimiter(','); csv.setUseTextQualifier(true); csv.readHeaders(); logger.debug("Count header: " + csv.getHeaderCount()); for (int i = 0; i < csv.getHeaderCount(); ++i) logger.debug("header: " + csv.getHeader(i)); // Hibernate-Block, Session ffnen org.hibernate.Session hb_session = HibernateUtil.getSessionFactory().openSession(); org.hibernate.Transaction tx = hb_session.beginTransaction(); try // try-catch-Block zum Abfangen von Hibernate-Fehlern { if (parameter.getCodeSystem() == null || parameter.getCodeSystem().getId() == 0 || parameter.getCodeSystem().getCodeSystemVersions() == null || parameter.getCodeSystem().getCodeSystemVersions().size() == 0) { // Fehlermeldung tx.rollback(); HibernateUtil.getSessionFactory().close(); return "No code system specified!"; } CodeSystemVersion codesystemVersion = (CodeSystemVersion) parameter.getCodeSystem() .getCodeSystemVersions().toArray()[0]; logger.debug("CSV-name: " + codesystemVersion.getName()); if ((codesystemVersion.getVersionId() == null || codesystemVersion.getVersionId() == 0) && codesystemVersion.getName() != null && codesystemVersion.getName().length() > 0) { // find codesystem version-id from name logger.debug("find codesystem version-id from name: " + codesystemVersion.getName()); Query q = hb_session.createQuery("select distinct csv from CodeSystemVersion csv" + " join csv.codeSystem" + " where csv.name=:version_name and id=:cs_id"); q.setString("version_name", codesystemVersion.getName()); q.setLong("cs_id", parameter.getCodeSystem().getId()); List<CodeSystemVersion> list = q.list(); if (list != null && list.size() > 0) { CodeSystemVersion csv_db = list.get(0); codesystemVersion.setVersionId(csv_db.getVersionId()); } } long csv_id = codesystemVersion.getVersionId(); logger.debug("codesystem version id: " + csv_id); // read associations associationMap = new HashMap<String, AssociationType>(); List<AssociationType> associationList = hb_session.createQuery("from AssociationType").list(); for (int i = 0; i < associationList.size(); ++i) { associationMap.put(associationList.get(i).getForwardName(), associationList.get(i)); } logger.debug("Start import..."); int countEvery = 0; // load data while (csv.readRecord()) { countEvery++; CreateConceptAssociationRequestType request = new CreateConceptAssociationRequestType(); request.setLoginToken(parameter.getLoginToken()); request.setCodeSystemEntityVersionAssociation(new CodeSystemEntityVersionAssociation()); String code1 = csv.get("LOINC"); String code2 = csv.get("MAP_TO"); String comment = csv.get("COMMENT"); // CSV-ID fr LOINC-Code lesen long csev_id1 = getCSEV_VersionIdFromCode(code1, csv_id, hb_session); long csev_id2 = getCSEV_VersionIdFromCode(code2, csv_id, hb_session); //logger.warn(new Date().getTime() + ", Codes gelesen"); if (csev_id1 > 0 && csev_id2 > 0) { // IDs setzen request.getCodeSystemEntityVersionAssociation() .setCodeSystemEntityVersionByCodeSystemEntityVersionId1( new CodeSystemEntityVersion()); request.getCodeSystemEntityVersionAssociation() .setCodeSystemEntityVersionByCodeSystemEntityVersionId2( new CodeSystemEntityVersion()); request.getCodeSystemEntityVersionAssociation() .getCodeSystemEntityVersionByCodeSystemEntityVersionId1().setVersionId(csev_id1); request.getCodeSystemEntityVersionAssociation() .getCodeSystemEntityVersionByCodeSystemEntityVersionId2().setVersionId(csev_id2); request.getCodeSystemEntityVersionAssociation().setAssociationKind(1); // ontologisch request.getCodeSystemEntityVersionAssociation().setLeftId(csev_id1); request.getCodeSystemEntityVersionAssociation().setAssociationType(new AssociationType()); // Assoziationstyp erhalten if (comment != null && comment.length() > 0 && associationMap.containsKey(comment)) { request.getCodeSystemEntityVersionAssociation().getAssociationType() .setCodeSystemEntityVersionId( associationMap.get(comment).getCodeSystemEntityVersionId()); } else { if (comment != null && comment.length() > 0) { // Neuen Beziehungstyp einfgen CreateConceptAssociationTypeRequestType requestAssociation = new CreateConceptAssociationTypeRequestType(); requestAssociation.setLoginToken(parameter.getLoginToken()); requestAssociation.setCodeSystem(parameter.getCodeSystem()); requestAssociation.setCodeSystemEntity(new CodeSystemEntity()); requestAssociation.getCodeSystemEntity() .setCodeSystemEntityVersions(new HashSet<CodeSystemEntityVersion>()); AssociationType at = new AssociationType(); if (comment.length() > 48) at.setForwardName(comment.substring(0, 48)); else at.setForwardName(comment); at.setReverseName(""); CodeSystemEntityVersion csev = new CodeSystemEntityVersion(); csev.setAssociationTypes(new HashSet<AssociationType>()); csev.getAssociationTypes().add(at); requestAssociation.getCodeSystemEntity().getCodeSystemEntityVersions().add(csev); CreateConceptAssociationType ccat = new CreateConceptAssociationType(); CreateConceptAssociationTypeResponseType responseAssociation = ccat .CreateConceptAssociationType(requestAssociation, hb_session, loginInfoType); if (responseAssociation.getReturnInfos().getStatus() == ReturnType.Status.OK) { CodeSystemEntityVersion csev_result = (CodeSystemEntityVersion) responseAssociation .getCodeSystemEntity().getCodeSystemEntityVersions().toArray()[0]; request.getCodeSystemEntityVersionAssociation().getAssociationType() .setCodeSystemEntityVersionId(csev_result.getVersionId()); } else request.getCodeSystemEntityVersionAssociation().getAssociationType() .setCodeSystemEntityVersionId(5l); // TODO gehrt zu } else { // Standard-Beziehung nehmen request.getCodeSystemEntityVersionAssociation().getAssociationType() .setCodeSystemEntityVersionId(5l); // TODO gehrt zu } } //logger.warn(new Date().getTime() + ", Assoziationstyp bestimmt"); StaticStatus.importCount++; if (StaticStatus.importCount % 100 == 0) logger.debug("Lese Datensatz " + StaticStatus.importCount + ", count: " + count); // Beziehung speichern // Dienst aufrufen (Konzept einfgen) CreateConceptAssociation cca = new CreateConceptAssociation(); CreateConceptAssociationResponseType responseCCA = cca.CreateConceptAssociation(request, hb_session, loginInfoType); if (responseCCA.getReturnInfos().getStatus() == ReturnType.Status.OK) { count++; if (StaticStatus.importCount % 100 == 0) logger.debug( "Neue Beziehung hinzugefgt, ID1: " + csev_id1 + ", ID2: " + csev_id2); } else countFehler++; //logger.warn(new Date().getTime() + ", Assoziation in DB eingefgt"); } else { countFehler++; logger.debug("Term ist nicht angegeben"); } //logger.warn(new Date().getTime() + ", vor Speicherbereinigung"); //Mimimum acceptable free memory you think your app needs //long minRunningMemory = (1024 * 1024); //Runtime runtime = Runtime.getRuntime(); if (countEvery % 500 == 0) { //logger.debug("FreeMemory: " + runtime.freeMemory()); // wichtig, sonst kommt es bei greren Dateien zum Java-Heapspace-Fehler hb_session.flush(); hb_session.clear(); if (countEvery % 1000 == 0) { // sicherheitshalber aufrufen System.gc(); } } //logger.warn(new Date().getTime() + ", nach Speicherbereinigung"); //logger.warn(new Date().getTime() + ", Lese nchsten Record"); csv.skipLine(); if (countFehler > 10) break; } if (count == 0 || countFehler > 10) { tx.rollback(); response.getReturnInfos().setMessage("No associations imported. Count errors: " + countFehler); } else { logger.debug("Import completed, speicher Ergebnisse in DB (commit): " + count); tx.commit(); countImported = count; response.getReturnInfos().setMessage( "Import completed. " + count + " associations imported, " + countFehler + " errors."); } } catch (Exception ex) { LoggingOutput.outputException(ex, this); logger.error("Error while importing LOINC-associations: " + ex.getMessage()); s = "Error while importing LOINC-associations: " + ex.getLocalizedMessage(); try { tx.rollback(); logger.info("[ImportLOINC.java] Rollback durchgefhrt!"); } catch (Exception exRollback) { logger.info(exRollback.getMessage()); logger.info("[ImportLOINC.java] Rollback fehlgeschlagen!"); } } finally { // Session schlieen HibernateUtil.getSessionFactory().close(); } } catch (Exception ex) { s = "Error while importing LOINC associations: " + ex.getLocalizedMessage(); LoggingOutput.outputException(ex, this); } associationMap.clear(); associationMap = null; //codesMap.clear(); //codesMap = null; logger.debug("ImportLOINC-associations - completed"); return s; }
From source file:de.fhdo.terminologie.ws.administration._import.ImportLOINC_V254.java
License:Apache License
public String importLOINC(ImportCodeSystemResponseType response) { StaticStatus.importCount = 0;/*www. j a v a 2 s . c o m*/ StaticStatus.importTotal = 0; String s = ""; int count = 0, countFehler = 0; //codesMap = new HashMap<String, Long>(); CsvReader csv; try { byte[] bytes = parameter.getImportInfos().getFilecontent(); logger.debug("convert to InputStream..."); InputStream is = new ByteArrayInputStream(bytes); csv = new CsvReader(is, Charset.forName("ISO-8859-1")); csv.setDelimiter(','); csv.setUseTextQualifier(true); csv.readHeaders(); logger.debug("Anzahl Header: " + csv.getHeaderCount()); // Hibernate-Block, Session ffnen org.hibernate.Session hb_session = HibernateUtil.getSessionFactory().openSession(); org.hibernate.Transaction tx = hb_session.beginTransaction(); try // try-catch-Block zum Abfangen von Hibernate-Fehlern { if (ImportCreateCodeSystem.createCodeSystem(hb_session, parameter, response) == false) { // Fehlermeldung tx.rollback(); return "Code system could not be created!"; } // Metadaten-Parameter lesen metadataParameterMap = new HashMap<String, MetadataParameter>(); List<MetadataParameter> mpList = hb_session.createQuery("from MetadataParameter").list(); for (int i = 0; i < mpList.size(); ++i) { metadataParameterMap.put(mpList.get(i).getParamName(), mpList.get(i)); } logger.debug("start import..."); // Request hier schon zusammenbauen (muss nicht tausendfach in der Schleife gemacht werden) // berlegen, was alles in der Schleife erzeugt wird (Speicherproblematik) while (csv.readRecord()) { CreateConceptRequestType request = new CreateConceptRequestType(); request.setLoginToken(parameter.getLoginToken()); request.setCodeSystem(parameter.getCodeSystem()); request.setCodeSystemEntity(new CodeSystemEntity()); request.getCodeSystemEntity() .setCodeSystemEntityVersions(new HashSet<CodeSystemEntityVersion>()); CodeSystemConcept csc = new CodeSystemConcept(); csc.setIsPreferred(true); CodeSystemEntityVersion csev = new CodeSystemEntityVersion(); csev.setCodeSystemConcepts(new HashSet<CodeSystemConcept>()); csev.setIsLeaf(true); CodeSystemConceptTranslation translation = new CodeSystemConceptTranslation(); translation.setLanguageCd(Definitions.LANGUAGE_ENGLISH_CD); CreateConcept cc = new CreateConcept(); StaticStatus.importCount++; if (StaticStatus.importCount % 200 == 0) logger.debug("read record " + StaticStatus.importCount + ", count: " + count); csc.setCode(csv.get("LOINC_NUM")); csc.setTerm(csv.get("COMPONENT")); csc.setTermAbbrevation(csv.get("SHORTNAME")); // Sprachen prfen (Englisch-bersetzung hinzufgen) csc.getCodeSystemConceptTranslations().clear(); String translationEnglish = csv.get("LONG_COMMON_NAME"); if (translationEnglish != null && translationEnglish.length() > 0) { translation.setTerm(translationEnglish); csc.getCodeSystemConceptTranslations().add(translation); } // fit data length if (csc.getCode().length() > 98) { csc.setCode(csc.getCode().substring(0, 98)); } if (csc.getTermAbbrevation().length() > 49) { csc.setTermAbbrevation(csc.getTermAbbrevation().substring(0, 49)); } // Entity-Version erstellen if (csev.getCodeSystemConcepts() == null) csev.setCodeSystemConcepts(new HashSet<CodeSystemConcept>()); else csev.getCodeSystemConcepts().clear(); csev.getCodeSystemConcepts().add(csc); csev.setEffectiveDate(parseDate(csv.get("DATE_LAST_CHANGED"))); csev.setStatusWorkflow(getLoincStatus(csv.get("STATUS"))); csev.setStatusVisibility(1); csev.setStatusVisibilityDate(new Date()); csev.setStatusWorkflowDate(new Date()); // Konzept speichern if (csc.getCode().length() > 0) { // Entity-Version dem Request hinzufgen request.getCodeSystemEntity().getCodeSystemEntityVersions().clear(); request.getCodeSystemEntity().getCodeSystemEntityVersions().add(csev); // Dienst aufrufen (Konzept einfgen) CreateConceptResponseType responseCC = cc.CreateConcept(request, hb_session, loginInfoType); if (responseCC.getReturnInfos().getStatus() == ReturnType.Status.OK) { count++; //if (responseCC.getCodeSystemEntity().getCurrentVersionId() > 0) // codesMap.put(csc.getCode(), responseCC.getCodeSystemEntity().getCurrentVersionId()); // Metadaten zu diesem Konzept speichern int mdCount = 0; CodeSystemEntityVersion csev_result = (CodeSystemEntityVersion) responseCC .getCodeSystemEntity().getCodeSystemEntityVersions().toArray()[0]; mdCount = addMetadataToConcept(csv, parameter.getCodeSystem().getId(), csev_result.getVersionId(), hb_session); //if (StaticStatus.importCount % 200 == 0) logger.debug("add new concept: " + csc.getCode() + ", count metadata: " + mdCount); } else countFehler++; } else { countFehler++; logger.debug("Term is not specified"); } //Mimimum acceptable free memory you think your app needs //long minRunningMemory = (1024 * 1024); Runtime runtime = Runtime.getRuntime(); if (StaticStatus.importCount % 200 == 0) { logger.debug("FreeMemory: " + runtime.freeMemory()); if (StaticStatus.importCount % 1000 == 0) { // wichtig, sonst kommt es bei greren Dateien zum Java-Heapspace-Fehler hb_session.flush(); hb_session.clear(); } if (StaticStatus.importCount % 10000 == 0) { // sicherheitshalber aufrufen System.gc(); } } } if (count == 0) { tx.rollback(); response.getReturnInfos().setMessage("No concepts imported."); } else { logger.debug("Import completed, speicher Ergebnisse in DB (commit): " + count); tx.commit(); countImported = count; response.getReturnInfos().setMessage( "Import completed. " + count + " concepts imported, " + countFehler + " errors"); } } catch (Exception ex) { LoggingOutput.outputException(ex, this); s = "Error while importing LOINC: " + ex.getLocalizedMessage(); try { tx.rollback(); logger.info("[ImportLOINC.java] Rollback durchgefhrt!"); } catch (Exception exRollback) { logger.info(exRollback.getMessage()); logger.info("[ImportLOINC.java] Rollback fehlgeschlagen!"); } } finally { // Session schlieen HibernateUtil.getSessionFactory().close(); } } catch (Exception ex) { s = "Error while importing LOINC: " + ex.getLocalizedMessage(); LoggingOutput.outputException(ex, this); } StaticStatus.importCount = 0; StaticStatus.importTotal = 0; logger.debug("ImportLOINC - fertig"); return s; }