List methods

This commit is contained in:
Fabio Sinibaldi 2020-11-05 18:25:25 +01:00
parent 64522f0527
commit 9a42faef00
5 changed files with 106 additions and 10 deletions

View File

@ -2,6 +2,11 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
# Changelog for org.gcube.application.geoportal-logic
## [v1.0.3-SNAPSHOT] - 2020-11-4
Implemented list, list by type
Fixed : commit transaction when safelyCommit project
## [v1.0.2] - 2020-11-4
PublicationReport

View File

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.gcube.application</groupId>
<artifactId>geoportal-logic</artifactId>
<version>1.0.2</version>
<version>1.0.3-SNAPSHOT</version>
<name>Geoportal Logic</name>
@ -36,7 +36,7 @@
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>gcube-bom</artifactId>
<version>2.0.0</version>
<version>2.0.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>

View File

@ -6,11 +6,13 @@ import java.net.URL;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.persistence.DiscriminatorValue;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
@ -124,7 +126,45 @@ public abstract class AbstractRecordManager<T extends Record> {
}
}
public static Collection<Record> getList(){
EntityManager em=getEMF().createEntityManager();
try {
log.debug("Getting entire list");
EntityTransaction tr=em.getTransaction();
tr.begin();
List<Record> toReturn=em.createQuery("select r from Record r ",
Record.class).getResultList();
log.debug("Loaded size "+toReturn.size());
tr.commit();
return toReturn;
}finally {
if(em.isJoinedToTransaction())
em.flush();
em.close();
}
}
public static <E extends Record> Collection<E> getListByClass(Class<E> clazz){
EntityManager em=getEMF().createEntityManager();
try {
log.debug("Getting entire list");
EntityTransaction tr=em.getTransaction();
tr.begin();
String simpleClassName=clazz.getName().substring(clazz.getName().lastIndexOf(".")+1);
List<E> toReturn=em.createQuery("select r from "+simpleClassName+" r",
clazz).getResultList();
log.debug("Loaded size "+toReturn.size());
tr.commit();
return toReturn;
}finally {
if(em.isJoinedToTransaction())
em.flush();
em.close();
}
}
// protected T storeInfo()
// {
// log.debug("Storing Record "+theRecord);
@ -227,12 +267,15 @@ public abstract class AbstractRecordManager<T extends Record> {
registerCentroid();
toReturn.addMessage(ValidationStatus.PASSED, "Inserito centroide per record "+theRecord.getId());
}
transaction.commit();
} catch (PersistenceException e) {
toReturn.addChild(e.getReport());
} catch (PublishException e) {
toReturn.addMessage(ValidationStatus.WARNING, "Centroide non registrato");
}
try {
log.info("Report is "+toReturn.prettyPrint());
}catch (Exception e) {
@ -250,6 +293,8 @@ public abstract class AbstractRecordManager<T extends Record> {
}
entityManager.flush();
entityManager.close();
shutdown();
}
private void registerCentroid() throws PublishException {

View File

@ -1,5 +1,7 @@
package org.gcube.application.geoportal.managers;
import java.util.Collection;
import org.gcube.application.geoportal.model.Record;
import org.gcube.application.geoportal.model.concessioni.Concessione;
@ -21,4 +23,12 @@ public class ManagerFactory {
public static <T extends Record,E extends AbstractRecordManager<T>> E registerNew(T toRegister) {
return getByRecord(toRegister);
}
public static Collection<Record> getList(){
return AbstractRecordManager.getList();
}
public static <T extends Record> Collection<T> getList(Class<T> clazz){
return AbstractRecordManager.getListByClass(clazz);
}
}

View File

@ -7,6 +7,7 @@ import java.io.IOException;
import org.gcube.application.geoportal.managers.ConcessioneManager;
import org.gcube.application.geoportal.managers.ManagerFactory;
import org.gcube.application.geoportal.model.InputStreamDescriptor;
import org.gcube.application.geoportal.model.Record;
import org.gcube.application.geoportal.model.concessioni.Concessione;
import org.gcube.application.geoportal.model.concessioni.LayerConcessione;
import org.gcube.application.geoportal.model.concessioni.RelazioneScavo;
@ -24,15 +25,32 @@ public class UseCases {
TokenSetter.set("/gcube/devNext/NextNext");
// TokenSetter.set("/pred4s/preprod/preVRE");
try {
// System.out.println("Try to create..");
// Concessione conc=registerNewConcessione();
try {
//CREATE NEW
// int numConcessioni=1;
// System.out.println("Try to create.. "+numConcessioni);
// for(int i=0;i<numConcessioni;i++)
// registerNewConcessione();
// long id=conc.getId();
//READ BY ID
// long id=48;
// System.out.println("Tryint to read by id "+id);
// readConcessione(id);
//GET LIST
System.out.println(getList());
//GET LIST BY TYPE
System.out.println(getConcessioneList());
// long id=conc.getId();
long id=48;
System.out.println("Tryint to read by id "+id);
readConcessione(id);
}catch(Throwable t) {
System.err.println("ALERT "+t.getMessage());
throw t;
@ -136,4 +154,22 @@ public class UseCases {
}
}
public static String getList(){
StringBuilder builder=new StringBuilder();
for(Record r : ManagerFactory.getList()) {
builder.append("Record ["+r.getId()+"\t\""+r.getNome()+"\t"+r.getRecordType()+"]");
}
return builder.toString();
}
public static String getConcessioneList(){
StringBuilder builder=new StringBuilder();
for(Concessione r : ManagerFactory.getList(Concessione.class)) {
builder.append("Concessione ["+r.getId()+"\t\""+r.getNome()+"\"\t"+r.getRecordType()+"\t : "+r.validate().getStatus()+"]");
}
return builder.toString();
}
}