Search in sources :

Example 11 with PropertyAccessor

use of com.manydesigns.elements.reflection.PropertyAccessor in project Portofino by ManyDesigns.

the class AbstractCrudAction method configureSortLinks.

protected void configureSortLinks(TableFormBuilder tableFormBuilder) {
    for (PropertyAccessor propertyAccessor : classAccessor.getProperties()) {
        String propName = propertyAccessor.getName();
        String sortDirection;
        if (propName.equals(sortProperty) && "asc".equals(this.sortDirection)) {
            sortDirection = "desc";
        } else {
            sortDirection = "asc";
        }
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("sortProperty", propName);
        parameters.put("sortDirection", sortDirection);
        parameters.put(SEARCH_STRING_PARAM, searchString);
        Charset charset = Charset.forName(context.getRequest().getCharacterEncoding());
        UrlBuilder urlBuilder = new UrlBuilder(charset, Util.getAbsoluteUrl(context.getActionPath()), false).addParameters(parameters);
        XhtmlBuffer xb = new XhtmlBuffer();
        xb.openElement("a");
        xb.addAttribute("class", "sort-link");
        xb.addAttribute("href", urlBuilder.toString());
        xb.writeNoHtmlEscape("%{label}");
        if (propName.equals(sortProperty)) {
            xb.openElement("em");
            xb.addAttribute("class", "pull-right glyphicon glyphicon-chevron-" + ("desc".equals(sortDirection) ? "up" : "down"));
            xb.closeElement("em");
        }
        xb.closeElement("a");
        OgnlTextFormat hrefFormat = OgnlTextFormat.create(xb.toString());
        String encoding = getUrlEncoding();
        hrefFormat.setEncoding(encoding);
        tableFormBuilder.configHeaderTextFormat(propName, hrefFormat);
    }
}
Also used : PropertyAccessor(com.manydesigns.elements.reflection.PropertyAccessor) XhtmlBuffer(com.manydesigns.elements.xml.XhtmlBuffer) Charset(java.nio.charset.Charset) JSONObject(org.json.JSONObject) UrlBuilder(com.manydesigns.elements.servlet.UrlBuilder) OgnlTextFormat(com.manydesigns.elements.text.OgnlTextFormat)

Example 12 with PropertyAccessor

use of com.manydesigns.elements.reflection.PropertyAccessor in project Portofino by ManyDesigns.

the class AbstractCrudAction method configureFormSelectionProviders.

protected void configureFormSelectionProviders(FormBuilder formBuilder) {
    if (selectionProviderSupport == null || selectionProviderLoadStrategy == SelectionProviderLoadStrategy.NONE) {
        return;
    }
    // setup option providers
    for (CrudSelectionProvider current : selectionProviderSupport.getCrudSelectionProviders()) {
        SelectionProvider selectionProvider = current.getSelectionProvider();
        if (selectionProvider == null || (selectionProviderLoadStrategy == SelectionProviderLoadStrategy.ONLY_ENFORCED && !current.isEnforced())) {
            continue;
        }
        String[] fieldNames = current.getFieldNames();
        if (object != null) {
            Object[] values = new Object[fieldNames.length];
            boolean valuesRead = true;
            for (int i = 0; i < fieldNames.length; i++) {
                String fieldName = fieldNames[i];
                try {
                    PropertyAccessor propertyAccessor = classAccessor.getProperty(fieldName);
                    values[i] = propertyAccessor.get(object);
                } catch (Exception e) {
                    logger.error("Couldn't read property " + fieldName, e);
                    valuesRead = false;
                }
            }
            if (valuesRead) {
                selectionProvider.ensureActive(values);
            }
        }
        formBuilder.configSelectionProvider(selectionProvider, fieldNames);
    }
}
Also used : PropertyAccessor(com.manydesigns.elements.reflection.PropertyAccessor) JSONObject(org.json.JSONObject) SelectionProvider(com.manydesigns.elements.options.SelectionProvider) URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException)

Example 13 with PropertyAccessor

use of com.manydesigns.elements.reflection.PropertyAccessor 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 14 with PropertyAccessor

use of com.manydesigns.elements.reflection.PropertyAccessor in project Portofino by ManyDesigns.

the class IdStrategy method getFormatString.

@NotNull
public String getFormatString() {
    StringBuilder sb = new StringBuilder();
    boolean first = true;
    for (PropertyAccessor property : classAccessor.getKeyProperties()) {
        if (first) {
            first = false;
        } else {
            sb.append("/");
        }
        sb.append("%{");
        sb.append(property.getName());
        sb.append("}");
    }
    return sb.toString();
}
Also used : PropertyAccessor(com.manydesigns.elements.reflection.PropertyAccessor) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with PropertyAccessor

use of com.manydesigns.elements.reflection.PropertyAccessor in project Portofino by ManyDesigns.

the class QueryUtils method getObjectByPk.

/**
 * Loads an object by primary key.
 * @param session the Hibernate session
 * @param table the table where to load the object from
 * @param pk the primary key object. Might be an atomic value (String, Integer, etc.) for single-column primary
 * keys, or a composite object for multi-column primary keys.
 * @return the loaded object, or null if an object with that key does not exist.
 */
public static Object getObjectByPk(Session session, TableAccessor table, Serializable pk) {
    String actualEntityName = table.getTable().getActualEntityName();
    Object result;
    PropertyAccessor[] keyProperties = table.getKeyProperties();
    int size = keyProperties.length;
    if (size > 1) {
        result = session.get(actualEntityName, pk);
        return result;
    }
    PropertyAccessor propertyAccessor = keyProperties[0];
    Serializable key = (Serializable) propertyAccessor.get(pk);
    result = session.get(actualEntityName, key);
    return result;
}
Also used : Serializable(java.io.Serializable) PropertyAccessor(com.manydesigns.elements.reflection.PropertyAccessor)

Aggregations

PropertyAccessor (com.manydesigns.elements.reflection.PropertyAccessor)46 ClassAccessor (com.manydesigns.elements.reflection.ClassAccessor)13 JavaClassAccessor (com.manydesigns.elements.reflection.JavaClassAccessor)12 JSONObject (org.json.JSONObject)5 SelectionProvider (com.manydesigns.elements.options.SelectionProvider)4 Field (com.manydesigns.elements.fields.Field)3 SelectField (com.manydesigns.elements.fields.SelectField)3 OgnlTextFormat (com.manydesigns.elements.text.OgnlTextFormat)3 TableAccessor (com.manydesigns.portofino.reflection.TableAccessor)3 Session (org.hibernate.Session)3 FieldSet (com.manydesigns.elements.annotations.FieldSet)2 SelectionModel (com.manydesigns.elements.options.SelectionModel)2 QueryStringWithParameters (com.manydesigns.elements.text.QueryStringWithParameters)2 TableCriteria (com.manydesigns.portofino.persistence.TableCriteria)2 SelectionProviderReference (com.manydesigns.portofino.resourceactions.m2m.configuration.SelectionProviderReference)2 Serializable (java.io.Serializable)2 Annotation (java.lang.annotation.Annotation)2 BigDecimal (java.math.BigDecimal)2 URISyntaxException (java.net.URISyntaxException)2 HashMap (java.util.HashMap)2