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);
}
}
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);
}
}
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;
}
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();
}
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;
}
Aggregations