Search in sources :

Example 1 with TextFormat

use of com.manydesigns.elements.text.TextFormat 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 2 with TextFormat

use of com.manydesigns.elements.text.TextFormat 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)

Example 3 with TextFormat

use of com.manydesigns.elements.text.TextFormat in project Portofino by ManyDesigns.

the class SelectionProviderLogic method getOption.

@NotNull
public static OptionProvider.Option getOption(String name, PropertyAccessor[] propertyAccessors, @Nullable TextFormat[] textFormats, Object object) {
    boolean active = true;
    if (object instanceof Object[]) {
        Object[] valueAndActive = (Object[]) object;
        if (valueAndActive.length > 1) {
            active = valueAndActive[1] instanceof Boolean && (Boolean) valueAndActive[1];
        }
        if (valueAndActive.length > 0) {
            object = valueAndActive[0];
        } else {
            throw new IllegalArgumentException("Invalid selection provider query result - sp: " + name);
        }
    }
    Object[] values = new Object[propertyAccessors.length];
    String[] labels = new String[propertyAccessors.length];
    int j = 0;
    for (PropertyAccessor property : propertyAccessors) {
        Object value = property.get(object);
        values[j] = value;
        if (textFormats == null || textFormats[j] == null) {
            String label = OgnlUtils.convertValueToString(value);
            labels[j] = label;
        } else {
            TextFormat textFormat = textFormats[j];
            labels[j] = textFormat.format(object);
        }
        j++;
    }
    return new OptionProvider.Option(values, labels, active);
}
Also used : PropertyAccessor(com.manydesigns.elements.reflection.PropertyAccessor) TextFormat(com.manydesigns.elements.text.TextFormat) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

TextFormat (com.manydesigns.elements.text.TextFormat)3 ShortName (com.manydesigns.elements.annotations.ShortName)2 OgnlTextFormat (com.manydesigns.elements.text.OgnlTextFormat)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 NotNull (org.jetbrains.annotations.NotNull)1 JSONObject (org.json.JSONObject)1