Search in sources :

Example 1 with Tuple

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);
    }
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) TreeSet(java.util.TreeSet) BroadleafEnumerationType(org.broadleafcommerce.common.BroadleafEnumerationType) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) Tuple(org.broadleafcommerce.common.util.Tuple)

Example 2 with Tuple

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;
}
Also used : Status(org.broadleafcommerce.common.persistence.Status) MetadataProviderResponse(org.broadleafcommerce.openadmin.server.service.type.MetadataProviderResponse) Media(org.broadleafcommerce.common.media.domain.Media) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) OneToMany(javax.persistence.OneToMany) FieldNotAvailableException(org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ParentEntityPersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException) PersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException) Field(java.lang.reflect.Field) FieldNotAvailableException(org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException) ParentEntityPersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException) PersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException) Tuple(org.broadleafcommerce.common.util.Tuple) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 3 with Tuple

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);
}
Also used : CategoryImpl(org.broadleafcommerce.core.catalog.domain.CategoryImpl) Category(org.broadleafcommerce.core.catalog.domain.Category) AdornedTargetList(org.broadleafcommerce.openadmin.dto.AdornedTargetList) Tuple(org.broadleafcommerce.common.util.Tuple)

Aggregations

Tuple (org.broadleafcommerce.common.util.Tuple)3 Field (java.lang.reflect.Field)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1 OneToMany (javax.persistence.OneToMany)1 BroadleafEnumerationType (org.broadleafcommerce.common.BroadleafEnumerationType)1 ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)1 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)1 Media (org.broadleafcommerce.common.media.domain.Media)1 Status (org.broadleafcommerce.common.persistence.Status)1 Category (org.broadleafcommerce.core.catalog.domain.Category)1 CategoryImpl (org.broadleafcommerce.core.catalog.domain.CategoryImpl)1 AdornedTargetList (org.broadleafcommerce.openadmin.dto.AdornedTargetList)1 ParentEntityPersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException)1 PersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException)1