use of com.manydesigns.elements.annotations.ShortName in project Portofino by ManyDesigns.
the class ShortNameUtils method getName.
public static String getName(ClassAccessor classAccessor, Object object) {
ShortName annotation = classAccessor.getAnnotation(ShortName.class);
String formatString;
if (annotation == null) {
StringBuilder sb = new StringBuilder();
boolean first = true;
// sintetizziamo una stringa a partire dalla chiave primaria
for (PropertyAccessor propertyAccessor : classAccessor.getKeyProperties()) {
if (first) {
first = false;
} else {
sb.append(PK_ELEMENT_SEPARATOR);
}
sb.append(String.format("%%{%s}", propertyAccessor.getName()));
}
formatString = sb.toString();
} else {
formatString = annotation.value();
}
OgnlTextFormat ognlTextFormat = OgnlTextFormat.create(formatString);
return ognlTextFormat.format(object);
}
use of com.manydesigns.elements.annotations.ShortName in project Portofino by ManyDesigns.
the class ModelSelectionProviderSupport method createHQLOptionProvider.
protected OptionProvider createHQLOptionProvider(DatabaseSelectionProvider selectionProvider, String name, String databaseName, String hql) {
Database database = DatabaseLogic.findDatabaseByName(persistence.getModel(), databaseName);
Table table = QueryUtils.getTableFromQueryString(database, hql);
if (table == null) {
logger.error("Selection provider {} has a HQL query that " + "refers to an entity that does not exist ({})", name, hql);
return null;
}
return new MemoizingOptionProvider(() -> {
String entityName = table.getActualEntityName();
Session session = persistence.getSession(databaseName);
QueryStringWithParameters queryWithParameters = QueryUtils.mergeQuery(hql, null, this);
Collection<Object> objects = getFromQueryCache(selectionProvider, queryWithParameters);
if (objects == null) {
String queryString = queryWithParameters.getQueryString();
Object[] parameters = queryWithParameters.getParameters();
logger.debug("Query not in cache: {}", queryString);
try {
objects = QueryUtils.runHqlQuery(session, queryString, parameters);
} catch (Exception e) {
logger.error("Exception in populating selection provider " + name, e);
return null;
}
putInQueryCache(selectionProvider, queryWithParameters, objects);
}
TableAccessor tableAccessor = persistence.getTableAccessor(databaseName, entityName);
ShortName shortNameAnnotation = tableAccessor.getAnnotation(ShortName.class);
TextFormat[] textFormats = null;
// L'ordinamento e' usato solo in caso di chiave singola
if (shortNameAnnotation != null && tableAccessor.getKeyProperties().length == 1) {
textFormats = new TextFormat[] { OgnlTextFormat.create(shortNameAnnotation.value()) };
}
final TextFormat[] actualTextFormats = textFormats;
Stream<OptionProvider.Option> optionStream = objects.stream().map(o -> SelectionProviderLogic.getOption(name, tableAccessor.getKeyProperties(), actualTextFormats, o));
if (selectionProvider instanceof ForeignKey) {
optionStream = optionStream.sorted(DefaultSelectionProvider.OPTION_COMPARATOR_BY_LABEL);
}
return optionStream.collect(Collectors.toList());
});
}
use of com.manydesigns.elements.annotations.ShortName in project Portofino by ManyDesigns.
the class ManyToManyAction method createSelectionProviderFromHql.
public DefaultSelectionProvider createSelectionProviderFromHql(String name, String databaseName, String hql, DisplayMode dm, SearchDisplayMode sdm) {
Database database = DatabaseLogic.findDatabaseByName(persistence.getModel(), databaseName);
Table table = QueryUtils.getTableFromQueryString(database, hql);
String entityName = table.getActualEntityName();
Session session = persistence.getSession(databaseName);
Collection<Object> objects = QueryUtils.getObjects(session, hql, null, null);
TableAccessor tableAccessor = persistence.getTableAccessor(databaseName, entityName);
ShortName shortNameAnnotation = tableAccessor.getAnnotation(ShortName.class);
TextFormat[] textFormats = null;
// L'ordinamento e' usato solo in caso di chiave singola
if (shortNameAnnotation != null && tableAccessor.getKeyProperties().length == 1) {
textFormats = new TextFormat[] { OgnlTextFormat.create(shortNameAnnotation.value()) };
}
DefaultSelectionProvider selectionProvider = SelectionProviderLogic.createSelectionProvider(name, objects, tableAccessor.getKeyProperties(), textFormats);
selectionProvider.setDisplayMode(dm);
selectionProvider.setSearchDisplayMode(sdm);
return selectionProvider;
}
Aggregations