![JAR search and dependency download from the Maven repository](/logo.png)
sdmx.gateway.services.DatabaseRegistry Maven / Gradle / Ivy
The newest version!
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package sdmx.gateway.services;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.FlushModeType;
import javax.persistence.Persistence;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.transaction.RollbackException;
import javax.validation.ConstraintViolationException;
import sdmx.Registry;
import sdmx.SdmxIO;
import sdmx.commonreferences.CodeReference;
import sdmx.commonreferences.CodelistReference;
import sdmx.commonreferences.ConceptReference;
import sdmx.commonreferences.ConceptSchemeReference;
import sdmx.commonreferences.DataStructureReference;
import sdmx.commonreferences.DataflowReference;
import sdmx.commonreferences.IDType;
import sdmx.commonreferences.ItemReference;
import sdmx.commonreferences.ItemSchemeReferenceBase;
import sdmx.commonreferences.NestedNCNameID;
import sdmx.commonreferences.Version;
import sdmx.exception.ParseException;
import sdmx.gateway.SdmxGatewayApplication;
import sdmx.gateway.entities.Codelist;
import sdmx.gateway.entities.Conceptscheme;
import sdmx.gateway.entities.Dataflow;
import sdmx.gateway.entities.Datastructure;
import sdmx.gateway.util.AnnotationsUtil;
import sdmx.gateway.util.CodeUtil;
import sdmx.gateway.util.CodelistUtil;
import sdmx.gateway.util.ConceptSchemeUtil;
import sdmx.gateway.util.ConceptUtil;
import sdmx.gateway.util.DataStructureUtil;
import sdmx.gateway.util.DataflowUtil;
import sdmx.gateway.util.LanguageUtil;
import sdmx.message.StructureType;
import sdmx.structure.base.ItemSchemeType;
import sdmx.structure.base.ItemType;
import sdmx.structure.codelist.CodeType;
import sdmx.structure.codelist.CodelistType;
import sdmx.structure.concept.ConceptSchemeType;
import sdmx.structure.concept.ConceptType;
import sdmx.structure.dataflow.DataflowType;
import sdmx.structure.datastructure.DataStructureType;
/**
*
* @author James
*/
public class DatabaseRegistry implements Registry {
public static final EntityManagerFactory EMF = Persistence.createEntityManagerFactory("sdmxgatewayPU");
EntityManager query = EMF.createEntityManager();
EntityManager update = EMF.createEntityManager();
public static void main(String args[]) throws IOException, ParseException {
}
public DatabaseRegistry() {
LanguageUtil.init(update);
}
@Override
public void load(StructureType struct) {
try {
System.out.println("Loading Codelists");
loadCodelists(struct);
} catch (Exception ex) {
ex.printStackTrace();
}
try {
System.out.println("Loading Concepts");
loadConcepts(struct);
} catch (Exception ex) {
ex.printStackTrace();
}
try {
System.out.println("Loading DataStructures");
loadDataStructures(struct);
} catch (Exception ex) {
ex.printStackTrace();
}
try {
System.out.println("Loading Dataflows");
loadDataflows(struct);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void loadDataflows(StructureType struct) {
if (struct.getStructures().getDataflows() == null) {
return;
}
update.setFlushMode(FlushModeType.AUTO);
update.getTransaction().begin();
for (int i = 0; i < struct.getStructures().getDataflows().size(); i++) {
DataflowType df = struct.getStructures().getDataflows().getDataflow(i);
Dataflow dbdf = DataflowUtil.createDatabaseDataflow(update, df);
update.merge(dbdf);
}
update.getTransaction().commit();
}
public void loadCodelists(StructureType struct) {
if (struct.getStructures().getCodelists() == null) {
return;
}
for (int i = 0; i < struct.getStructures().getCodelists().getCodelists().size(); i++) {
CodelistType c = struct.getStructures().getCodelists().getCodelists().get(i);
List clist = new ArrayList();
for (ItemType itm : c.getItems()) {
clist.add((CodeType) itm);
}
boolean alreadyExists = false;
sdmx.gateway.entities.Codelist codelist = CodelistUtil.findDatabaseCodelist(update, c.getAgencyID().toString(), c.getId().toString(), c.getVersion().toString());
if (codelist != null) {
alreadyExists = true;
System.out.println("Codelist: " + c.getAgencyID().toString() + ":" + c.getId().toString() + ":" + c.getVersion().toString() + " already exists");
}
sdmx.gateway.entities.Code cdb = null;
for (int j = 0; j < c.size(); j++) {
cdb = CodeUtil.findDatabaseCode(update, c.getAgencyID().toString(), c.getId().toString(), c.getVersion().toString(), c.getItem(j).getId().toString());
if (cdb != null) {
alreadyExists = true;
Iterator it = clist.iterator();
while (it.hasNext()) {
CodeType ct = it.next();
if (ct.getId().equals(cdb.getCodePK().getId())) {
it.remove();
}
}
}
}
if (alreadyExists) {
return;
/*
try {
update.getTransaction().begin();
sdmx.gateway.entities.Codelist cs = CodelistUtil.findDatabaseCodelist(update, c.getAgencyID().toString(), c.getId().toString(), c.getVersion().toString());
// What isn't already in the database
for (CodeType ct : clist) {
sdmx.gateway.entities.Code con = CodeUtil.createDatabaseCode(update, cs, ct);
cs.getCodeList().add(con);
con.setCodelist(cs);
}
update.merge(cs);
update.getTransaction().commit();
} catch (Exception ex) {
update.getTransaction().rollback();
ex.printStackTrace();
} finally {
update.clear();
}*/
} else {
try {
update.getTransaction().begin();
sdmx.gateway.entities.Codelist cs = CodelistUtil.createDatabaseCodelist(update, c);
update.persist(cs);
update.getTransaction().commit();
} catch (Exception ex) {
ex.printStackTrace();
update.getTransaction().rollback();
} finally {
update.clear();
}
}
}
}
public void loadConcepts(StructureType struct) {
if (struct.getStructures().getConcepts() == null) {
return;
}
for (int i = 0; i < struct.getStructures().getConcepts().getConceptSchemes().size(); i++) {
ConceptSchemeType c = struct.getStructures().getConcepts().getConceptSchemes().get(i);
List clist = new ArrayList();
for (ItemType itm : c.getItems()) {
clist.add((ConceptType) itm);
}
boolean alreadyExists = false;
sdmx.gateway.entities.Conceptscheme conceptscheme = ConceptSchemeUtil.findDatabaseConceptScheme(update, c.getAgencyID().toString(), c.getId().toString(), c.getVersion().toString());
if (conceptscheme != null) {
alreadyExists = true;
}
if (alreadyExists) {
List addList = new ArrayList<>();
sdmx.gateway.entities.Concept cdb = null;
for (int j = 0; j < c.size(); j++) {
cdb = ConceptUtil.findDatabaseConcept(update, c.getAgencyID().toString(), c.getId().toString(), c.getVersion().toString(), c.getItem(j).getId().toString());
if (cdb == null) {
addList.add((ConceptType) c.getItem(j));
}
}
try {
update.getTransaction().begin();
sdmx.gateway.entities.Conceptscheme cs = ConceptSchemeUtil.findDatabaseConceptScheme(update, c.getAgencyID().toString(), c.getId().toString(), c.getVersion().toString());
// What isn't already in the database
for (ConceptType ct : addList) {
sdmx.gateway.entities.Concept concept = ConceptUtil.createDatabaseConcept(update, conceptscheme, ct);
conceptscheme.getConceptList().add(concept);
concept.setConceptscheme(conceptscheme);
}
update.merge(cs);
update.getTransaction().commit();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (update.getTransaction().isActive()) {
update.getTransaction().rollback();
}
update.clear();
}
} else {
try {
update.getTransaction().begin();
sdmx.gateway.entities.Conceptscheme cs = ConceptSchemeUtil.createDatabaseConceptScheme(update, c);
update.persist(cs);
update.getTransaction().commit();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (update.getTransaction().isActive()) {
update.getTransaction().rollback();
}
update.clear();
}
}
}
}
public void loadDataStructures(StructureType struct) {
if (struct.getStructures().getDataStructures() == null) {
System.out.println("Data Structures Is Null");
return;
}
for (int i = 0; i < struct.getStructures().getDataStructures().getDataStructures().size(); i++) {
boolean alreadyExists = false;
try {
DataStructureType ds = struct.getStructures().getDataStructures().getDataStructures().get(i);
System.out.println("DS " + ds.getAgencyID().toString() + ":" + ds.getId().toString() + ":" + ds.getVersion().toString());
sdmx.gateway.entities.Datastructure ds2;
ds2 = DataStructureUtil.findDataStructure(update, ds.getAgencyID().toString(), ds.getId().toString(), ds.getVersion().toString());
if (ds2 != null) {
continue;
}
update.getTransaction().begin();
ds2 = DataStructureUtil.createDatabaseDataStructure(update, ds);
update.persist(ds2);
update.flush();
update.getTransaction().commit();
} catch (ConstraintViolationException e) {
e.getConstraintViolations().forEach(err -> System.out.println(err.toString()));
if (update.getTransaction().isActive()) {
update.getTransaction().rollback();
}
} catch (javax.persistence.RollbackException re) {
for (int j = 0; j < re.getSuppressed().length; j++) {
re.getSuppressed()[j].printStackTrace();
}
re.printStackTrace();
if (update.getTransaction().isActive()) {
update.getTransaction().rollback();
}
} catch (Exception ex) {
ex.printStackTrace();
if (update.getTransaction().isActive()) {
update.getTransaction().rollback();
}
} finally {
update.clear();
}
}
}
@Override
public void unload(StructureType struct) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void clear() {
}
@Override
public List listDataflows() {
List dataflows = search(DataflowReference.create(new NestedNCNameID("all"), new IDType("all"), new Version("*")));
return dataflows;
}
@Override
public DataStructureType find(DataStructureReference ref) {
return DataStructureUtil.toSDMXDataStructure(DataStructureUtil.findDataStructure(query, ref.getAgencyId().toString(), ref.getMaintainableParentId().toString(), ref.getVersion().toString()));
}
@Override
public DataflowType find(DataflowReference ref) {
Dataflow result = DataflowUtil.findDataflow(query, ref.getAgencyId().toString(), ref.getMaintainableParentId().toString(), ref.getVersion().toString());
DataflowType sdmxDataflow = DataflowUtil.toSDMXDataflow(result);
return sdmxDataflow;
}
@Override
public CodeType find(CodeReference ref) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public CodelistType find(CodelistReference ref) {
return CodelistUtil.toSDMXCodelist(CodelistUtil.findDatabaseCodelist(this.query, ref.getAgencyId().toString(), ref.getMaintainableParentId().toString(), ref.getVersion().toString()));
}
@Override
public ItemType find(ItemReference ref) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public ItemSchemeType find(ItemSchemeReferenceBase ref) {
CodelistType ct2 = find(ref.asCodelistReference());
if (ct2 != null) {
return ct2;
}
ConceptSchemeType ct = find(ref.asConceptSchemeReference());
if (ct != null) {
return ct;
}
return null;
}
@Override
public ConceptType find(ConceptReference ref) {
Conceptscheme cs = ConceptSchemeUtil.findDatabaseConceptScheme(query, ref.getAgencyId().toString(), ref.getMaintainableParentId().toString(), ref.getVersion().toString());
for (int i = 0; i < cs.getConceptList().size(); i++) {
if (cs.getConceptList().get(i).getConceptPK().getConceptid().equals(ref.getId().toString())) {
return ConceptUtil.toSDMXConcept(cs.getConceptList().get(i));
}
}
return null;
}
@Override
public ConceptSchemeType find(ConceptSchemeReference ref) {
List css = this.search(ref);
if (css.size() > 0) {
return css.get(0);
} else {
System.out.println("Cant find concept scheme:" + ref.getMaintainableParentId().toString());
return null;
}
}
@Override
public List search(DataStructureReference ref) {
//ref.dump();
List list = DataStructureUtil.searchDataStructure(query, ref.getAgencyId().toString(), ref.getMaintainableParentId().toString(), ref.getVersion().toString());
List result = new ArrayList();
for (Datastructure ds : list) {
result.add(DataStructureUtil.toSDMXDataStructure(ds));
}
return result;
}
@Override
public List search(DataflowReference ref) {
List list = DataflowUtil.searchDataflow(query, ref.getAgencyId().toString(), ref.getMaintainableParentId().toString(), ref.getVersion().toString());
List result = new ArrayList();
for (Dataflow df : list) {
result.add(DataflowUtil.toSDMXDataflow(df));
}
return result;
}
@Override
public List search(CodeReference ref) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public List search(CodelistReference ref) {
return CodelistUtil.toSDMXCodelist(CodelistUtil.searchCodelist(this.query, ref.getAgencyId().toString(), ref.getMaintainableParentId().toString(), ref.getVersion().toString()));
}
@Override
public List search(ItemReference ref) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public List search(ItemSchemeReferenceBase ref) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public List search(ConceptReference ref) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public List search(ConceptSchemeReference ref) {
return ConceptSchemeUtil.toSDMXConceptSchemeTypes(ConceptSchemeUtil.searchConceptScheme(this.query, ref.getAgencyId().toString(), ref.getMaintainableParentId().toString(), ref.getVersion().toString()));
}
@Override
public void save(OutputStream out) throws IOException {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public List getCache() {
return Collections.EMPTY_LIST;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy