Search in sources :

Example 1 with ShortName

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);
}
Also used : PropertyAccessor(com.manydesigns.elements.reflection.PropertyAccessor) ShortName(com.manydesigns.elements.annotations.ShortName) OgnlTextFormat(com.manydesigns.elements.text.OgnlTextFormat)

Example 2 with ShortName

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());
    });
}
Also used : ShortName(com.manydesigns.elements.annotations.ShortName) QueryStringWithParameters(com.manydesigns.elements.text.QueryStringWithParameters) TableAccessor(com.manydesigns.portofino.reflection.TableAccessor) TextFormat(com.manydesigns.elements.text.TextFormat) OgnlTextFormat(com.manydesigns.elements.text.OgnlTextFormat) Session(org.hibernate.Session)

Example 3 with ShortName

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;
}
Also used : TableAccessor(com.manydesigns.portofino.reflection.TableAccessor) TextFormat(com.manydesigns.elements.text.TextFormat) OgnlTextFormat(com.manydesigns.elements.text.OgnlTextFormat) JSONObject(org.json.JSONObject) ShortName(com.manydesigns.elements.annotations.ShortName) Session(org.hibernate.Session)

Aggregations

ShortName (com.manydesigns.elements.annotations.ShortName)3 OgnlTextFormat (com.manydesigns.elements.text.OgnlTextFormat)3 TextFormat (com.manydesigns.elements.text.TextFormat)2 TableAccessor (com.manydesigns.portofino.reflection.TableAccessor)2 Session (org.hibernate.Session)2 PropertyAccessor (com.manydesigns.elements.reflection.PropertyAccessor)1 QueryStringWithParameters (com.manydesigns.elements.text.QueryStringWithParameters)1 JSONObject (org.json.JSONObject)1