use of org.broadleafcommerce.common.util.Tuple in project BroadleafCommerce by BroadleafCommerce.
the class BroadleafEnumerationUtility method getEnumerationValues.
@SuppressWarnings("rawtypes")
public List<Tuple<String, String>> getEnumerationValues(String broadleafEnumerationClass, DynamicEntityDao dynamicEntityDao) {
try {
Map<String, String> enumVals;
Class<?> broadleafEnumeration = Class.forName(broadleafEnumerationClass);
Method typeMethod = broadleafEnumeration.getMethod("getType");
Method friendlyTypeMethod = broadleafEnumeration.getMethod("getFriendlyType");
Field types = dynamicEntityDao.getFieldManager().getField(broadleafEnumeration, "TYPES");
if (Comparable.class.isAssignableFrom(broadleafEnumeration)) {
enumVals = new LinkedHashMap<String, String>();
Set<BroadleafEnumerationType> blcEnumSet = new TreeSet<BroadleafEnumerationType>();
if (types != null) {
Map typesMap = (Map) types.get(null);
for (Object value : typesMap.values()) {
blcEnumSet.add((BroadleafEnumerationType) value);
}
for (Object value : typesMap.values()) {
enumVals.put((String) friendlyTypeMethod.invoke(value), (String) typeMethod.invoke(value));
}
}
} else {
enumVals = new TreeMap<String, String>();
if (types != null) {
Map typesMap = (Map) types.get(null);
for (Object value : typesMap.values()) {
enumVals.put((String) friendlyTypeMethod.invoke(value), (String) typeMethod.invoke(value));
}
} else {
Field[] fields = dynamicEntityDao.getAllFields(broadleafEnumeration);
for (Field field : fields) {
boolean isStatic = Modifier.isStatic(field.getModifiers());
if (isStatic && field.getType().isAssignableFrom(broadleafEnumeration)) {
enumVals.put((String) friendlyTypeMethod.invoke(field.get(null)), (String) typeMethod.invoke(field.get(null)));
}
}
}
}
List<Tuple<String, String>> enumerationValues = new ArrayList<Tuple<String, String>>();
for (String key : enumVals.keySet()) {
Tuple<String, String> t = new Tuple<String, String>(enumVals.get(key), key);
enumerationValues.add(t);
}
return enumerationValues;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.broadleafcommerce.common.util.Tuple in project BroadleafCommerce by BroadleafCommerce.
the class MediaFieldPersistenceProvider method populateValue.
@Override
public MetadataProviderResponse populateValue(PopulateValueRequest populateValueRequest, Serializable instance) throws PersistenceException {
if (!canHandlePersistence(populateValueRequest, instance)) {
return MetadataProviderResponse.NOT_HANDLED;
}
String prop = populateValueRequest.getProperty().getName();
if (prop.contains(FieldManager.MAPFIELDSEPARATOR)) {
Field field = populateValueRequest.getFieldManager().getField(instance.getClass(), prop.substring(0, prop.indexOf(FieldManager.MAPFIELDSEPARATOR)));
if (field.getAnnotation(OneToMany.class) == null) {
throw new UnsupportedOperationException("MediaFieldPersistenceProvider is currently only compatible with map fields when modelled using @OneToMany");
}
}
MetadataProviderResponse response = MetadataProviderResponse.HANDLED;
boolean dirty = false;
try {
setNonDisplayableValues(populateValueRequest);
Class<?> valueType = getStartingValueType(populateValueRequest);
if (Media.class.isAssignableFrom(valueType)) {
Media newMedia = mediaBuilderService.convertJsonToMedia(populateValueRequest.getProperty().getUnHtmlEncodedValue(), valueType);
boolean persist = false;
boolean noPrimary = false;
boolean update = false;
Media media;
Boolean cleared;
try {
checkExtension: {
if (extensionManager != null) {
ExtensionResultHolder<Tuple<Media, Boolean>> result = new ExtensionResultHolder<Tuple<Media, Boolean>>();
ExtensionResultStatusType statusType = extensionManager.getProxy().retrieveMedia(instance, populateValueRequest, result);
if (ExtensionResultStatusType.NOT_HANDLED != statusType) {
Tuple<Media, Boolean> tuple = result.getResult();
media = tuple.getFirst();
cleared = tuple.getSecond();
break checkExtension;
}
}
media = (Media) populateValueRequest.getFieldManager().getFieldValue(instance, populateValueRequest.getProperty().getName());
cleared = false;
}
if (newMedia == null) {
noPrimary = true;
dirty = true;
update = false;
if (!cleared && media != null) {
// remove entry in sku to media map
populateValueRequest.getFieldManager().setFieldValue(instance, populateValueRequest.getProperty().getName(), null);
populateValueRequest.getPersistenceManager().getDynamicEntityDao().remove(media);
}
} else if (media == null) {
media = (Media) valueType.newInstance();
mediaBuilderService.instantiateMediaFields(media);
Object parent = extractParent(populateValueRequest, instance);
populateValueRequest.getFieldManager().setFieldValue(media, populateValueRequest.getMetadata().getToOneParentProperty(), parent);
populateValueRequest.getFieldManager().setFieldValue(media, populateValueRequest.getMetadata().getMapKeyValueProperty(), prop.substring(prop.indexOf(FieldManager.MAPFIELDSEPARATOR) + FieldManager.MAPFIELDSEPARATOR.length(), prop.length()));
persist = true;
}
} catch (FieldNotAvailableException e) {
throw new IllegalArgumentException(e);
}
if (media != null) {
if (media instanceof Status && 'Y' == ((Status) media).getArchived()) {
persist = true;
}
populateValueRequest.getProperty().setOriginalValue(convertMediaToJson(media));
}
if (!noPrimary) {
dirty = establishDirtyState(newMedia, media);
update = dirty;
}
if (dirty) {
if (update) {
updateMedia(populateValueRequest, newMedia, persist, media);
}
response = MetadataProviderResponse.HANDLED_BREAK;
}
} else {
throw new UnsupportedOperationException("MediaFields only work with Media types.");
}
} catch (Exception e) {
throw ExceptionHelper.refineException(PersistenceException.class, PersistenceException.class, e);
}
populateValueRequest.getProperty().setIsDirty(dirty);
return response;
}
use of org.broadleafcommerce.common.util.Tuple in project BroadleafCommerce by BroadleafCommerce.
the class ChildCategoriesCustomPersistenceHandler method getChildAndParentCategories.
protected Tuple<Category, Category> getChildAndParentCategories(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao) {
AdornedTargetList adornedTargetList = (AdornedTargetList) persistencePackage.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST);
final String targetPath = adornedTargetList.getTargetObjectPath() + "." + adornedTargetList.getTargetIdProperty();
final String linkedPath = adornedTargetList.getLinkedObjectPath() + "." + adornedTargetList.getLinkedIdProperty();
Long parentId = Long.parseLong(persistencePackage.getEntity().findProperty(linkedPath).getValue());
Long childId = Long.parseLong(persistencePackage.getEntity().findProperty(targetPath).getValue());
Category parent = (Category) dynamicEntityDao.retrieve(CategoryImpl.class, parentId);
Category child = (Category) dynamicEntityDao.retrieve(CategoryImpl.class, childId);
return new Tuple<>(parent, child);
}
Aggregations