Search in sources :

Example 1 with TableCriteria

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);
    }
}
Also used : TableAccessor(com.manydesigns.portofino.reflection.TableAccessor) TableCriteria(com.manydesigns.portofino.persistence.TableCriteria) BigInteger(java.math.BigInteger) FileObject(org.apache.commons.vfs2.FileObject) Session(org.hibernate.Session)

Example 2 with TableCriteria

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();
}
Also used : TableAccessor(com.manydesigns.portofino.reflection.TableAccessor) TableCriteria(com.manydesigns.portofino.persistence.TableCriteria) FileObject(org.apache.commons.vfs2.FileObject) Session(org.hibernate.Session)

Example 3 with TableCriteria

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();
}
Also used : QueryStringWithParameters(com.manydesigns.elements.text.QueryStringWithParameters) TableCriteria(com.manydesigns.portofino.persistence.TableCriteria) JSQLParserException(net.sf.jsqlparser.JSQLParserException)

Example 4 with TableCriteria

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;
}
Also used : PropertyAccessor(com.manydesigns.elements.reflection.PropertyAccessor) TableCriteria(com.manydesigns.portofino.persistence.TableCriteria)

Example 5 with TableCriteria

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"));
}
Also used : TableAccessor(com.manydesigns.portofino.reflection.TableAccessor) TableCriteria(com.manydesigns.portofino.persistence.TableCriteria) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) FileObject(org.apache.commons.vfs2.FileObject) Session(org.hibernate.Session)

Aggregations

TableCriteria (com.manydesigns.portofino.persistence.TableCriteria)7 TableAccessor (com.manydesigns.portofino.reflection.TableAccessor)4 Session (org.hibernate.Session)4 QueryStringWithParameters (com.manydesigns.elements.text.QueryStringWithParameters)3 FileObject (org.apache.commons.vfs2.FileObject)3 PropertyAccessor (com.manydesigns.elements.reflection.PropertyAccessor)2 Model (com.manydesigns.portofino.model.Model)1 SelectionProviderReference (com.manydesigns.portofino.resourceactions.m2m.configuration.SelectionProviderReference)1 BigInteger (java.math.BigInteger)1 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1 JSQLParserException (net.sf.jsqlparser.JSQLParserException)1 PropertiesConfiguration (org.apache.commons.configuration2.PropertiesConfiguration)1 JSONObject (org.json.JSONObject)1