use of com.manydesigns.portofino.persistence.TableCriteria in project Portofino by ManyDesigns.
the class PersistenceTest method testAutoIncrementGenerator.
public void testAutoIncrementGenerator() {
Map<String, Object> supplierData = new HashMap<>();
supplierData.put("status", "99");
supplierData.put("name", "Giampiero");
Object supplier = makeEntity("jpetstore.public.Supplier", supplierData);
Session session = persistence.getSession("jpetstore");
session.save("supplier", supplier);
session.getTransaction().commit();
Table table = DatabaseLogic.findTableByName(persistence.getModel(), "jpetstore", "PUBLIC", "SUPPLIER");
assertNotNull(table);
TableAccessor tableAccessor = new TableAccessor(table);
TableCriteria criteria = new TableCriteria(table);
final BigInteger expectedId = new BigInteger("3");
try {
criteria.eq(tableAccessor.getProperty("suppid"), expectedId);
List<?> listObjs = QueryUtils.getObjects(session, criteria, null, null);
assertEquals(1, listObjs.size());
Object supp = listObjs.get(0);
String name = get(supp, "name");
assertEquals("Giampiero", name);
} catch (NoSuchFieldException e) {
fail(e.getMessage(), e);
}
}
use of com.manydesigns.portofino.persistence.TableCriteria in project Portofino by ManyDesigns.
the class PersistenceTest method testSearchAndUpdateCategorie.
public void testSearchAndUpdateCategorie() {
Table table = DatabaseLogic.findTableByName(persistence.getModel(), "jpetstore", "PUBLIC", "CATEGORY");
assertNotNull(table);
TableAccessor tableAccessor = new TableAccessor(table);
TableCriteria tableCriteria = new TableCriteria(table);
Session session = persistence.getSession("jpetstore");
CriteriaQuery<Object> criteria = QueryUtils.createCriteria(session, "category").query;
List<Object> resultCat = new ArrayList<>(session.createQuery(criteria).list());
int sizeCat = resultCat.size();
assertEquals("categorie", 5, sizeCat);
Object categoria0 = findCategory(tableAccessor, tableCriteria);
assertEquals("Fish", get(categoria0, "name"));
set(categoria0, "name", "Pesciu");
session.update("category", categoria0);
session.getTransaction().commit();
persistence.closeSessions();
// Controllo l'aggiornamento e riporto le cose come stavano
tableCriteria = new TableCriteria(table);
categoria0 = findCategory(tableAccessor, tableCriteria);
assertEquals("Pesciu", get(categoria0, "name"));
set(categoria0, "name", "Fish");
session = persistence.getSession("jpetstore");
session.update("category", categoria0);
session.getTransaction().commit();
persistence.closeSessions();
}
use of com.manydesigns.portofino.persistence.TableCriteria in project Portofino by ManyDesigns.
the class CrudAction method calculateTotalSearchRecords.
protected long calculateTotalSearchRecords() {
TableCriteria criteria = new TableCriteria(baseTable);
if (searchForm != null) {
searchForm.configureCriteria(criteria);
}
QueryStringWithParameters query = QueryUtils.mergeQuery(getBaseQuery(), criteria, this);
String queryString = query.getQueryString();
String totalRecordsQueryString;
try {
totalRecordsQueryString = generateCountQuery(queryString);
} catch (JSQLParserException e) {
throw new Error(e);
}
// TODO gestire count non disponibile (totalRecordsQueryString == null)
List<Object> result = QueryUtils.runHqlQuery(session, totalRecordsQueryString, query.getParameters());
return totalSearchRecords = ((Number) result.get(0)).longValue();
}
use of com.manydesigns.portofino.persistence.TableCriteria in project Portofino by ManyDesigns.
the class CrudAction method loadObjects.
@SuppressWarnings({ "rawtypes", "unchecked" })
public List<T> loadObjects() {
try {
TableCriteria criteria = new TableCriteria(baseTable);
if (searchForm != null) {
searchForm.configureCriteria(criteria);
}
if (!StringUtils.isBlank(sortProperty) && !StringUtils.isBlank(sortDirection)) {
try {
PropertyAccessor orderByProperty = getOrderByProperty(sortProperty);
if (orderByProperty != null)
criteria.orderBy(orderByProperty, sortDirection);
} catch (NoSuchFieldException e) {
logger.error("Can't order by " + sortProperty + ", property accessor not found", e);
}
}
objects = (List) QueryUtils.getObjects(session, getBaseQuery(), criteria, this, firstResult, maxResults);
} catch (ClassCastException e) {
objects = new ArrayList<>();
logger.warn("Incorrect Field Type", e);
RequestMessages.addWarningMessage(ElementsThreadLocals.getText("incorrect.field.type"));
}
return objects;
}
use of com.manydesigns.portofino.persistence.TableCriteria in project Portofino by ManyDesigns.
the class PersistenceTest method testSearchAndReadCategorieProdotti.
public void testSearchAndReadCategorieProdotti() {
Session session = persistence.getSession("jpetstore");
CriteriaQuery criteria = QueryUtils.createCriteria(session, "category").query;
List resultCat = new ArrayList(session.createQuery(criteria).list());
int sizeCat = resultCat.size();
assertEquals("categorie", 5, sizeCat);
resultCat.forEach(cat -> {
assertNotNull(get(cat, "name"));
});
criteria = QueryUtils.createCriteria(session, "product").query;
List resultProd = new ArrayList(session.createQuery(criteria).list());
Table table = DatabaseLogic.findTableByName(persistence.getModel(), "jpetstore", "PUBLIC", "CATEGORY");
TableAccessor tableAccessor = new TableAccessor(table);
TableCriteria tableCriteria = new TableCriteria(table);
findCategory(tableAccessor, tableCriteria);
int sizePrd = resultProd.size();
assertEquals("prodotti", 16, sizePrd);
Object prd0 = resultProd.get(0);
assertEquals("FI-SW-01", get(prd0, "productid"));
assertEquals("Angelfish", get(prd0, "name"));
}
Aggregations