@Override public void setValue(String name, Object value, boolean checkEquals) { Object oldValue = getValue(name); if ((!checkEquals) || (!InstanceUtils.propertyValueEquals(oldValue, value))) { properties.put(name, value); propertyChanged(name, oldValue, value); } }
/** * Loads a list of entities. */ public List<T> list() { ValueLoadContext loadContext = createLoadContext(); return dataManager.loadValues(loadContext).stream() .map(e -> castValue(e.getValue(PROP_NAME))) .collect(Collectors.toList()); }
/** * Loads a single instance and wraps it in Optional. */ public Optional<T> optional() { ValueLoadContext loadContext = createLoadContext(); loadContext.getQuery().setMaxResults(1); List<KeyValueEntity> list = dataManager.loadValues(loadContext); return list.isEmpty() ? Optional.empty() : Optional.ofNullable(castValue(list.get(0).getValue(PROP_NAME))); }
/** * Loads a single instance. * * @throws IllegalStateException if nothing was loaded */ public T one() { ValueLoadContext loadContext = createLoadContext(); loadContext.getQuery().setMaxResults(1); List<KeyValueEntity> list = dataManager.loadValues(loadContext); if (!list.isEmpty()) return castValue(list.get(0).getValue(PROP_NAME)); else throw new IllegalStateException("No results"); }
protected void convertEnumValues(KeyValueEntity entity, List<MetaProperty> enumProperties) { try { for (MetaProperty enumProperty : enumProperties) { Object enumValue = entity.getValue(enumProperty.getName()); if (enumValue != null) { Enumeration enumeration = enumProperty.getRange().asEnumeration(); entity.setValue(enumProperty.getName(), enumeration.parse(String.valueOf(enumValue))); } } } catch (ParseException e) { throw new RuntimeException("Unable to convert enum id to enum instance for EnumClass"); } } }
@SuppressWarnings("unchecked") @Override public int getCount() { if (loader == null) { return container.getItems().size(); } if (loader instanceof CollectionLoader) { return (int) dataManager.getCount(((CollectionLoader) loader).createLoadContext()); } else if (loader instanceof KeyValueCollectionLoader) { ValueLoadContext context = ((KeyValueCollectionLoader) loader).createLoadContext(); QueryTransformer transformer = QueryTransformerFactory.createTransformer(context.getQuery().getQueryString()); // TODO it doesn't work for query containing scalars in select transformer.replaceWithCount(); context.getQuery().setQueryString(transformer.getResult()); context.setProperties(Collections.singletonList("cnt")); List<KeyValueEntity> list = dataManager.loadValues(context); Number count = list.get(0).getValue("cnt"); return count == null ? 0 : count.intValue(); } else { log.warn("Unsupported loader type: {}", loader.getClass().getName()); return 0; } }