Search in sources :

Example 11 with AdornedTargetList

use of org.broadleafcommerce.openadmin.dto.AdornedTargetList in project BroadleafCommerce by BroadleafCommerce.

the class AdornedTargetListPersistenceModule method update.

@Override
public Entity update(PersistencePackage persistencePackage) throws ServiceException {
    String[] customCriteria = persistencePackage.getCustomCriteria();
    if (customCriteria != null && customCriteria.length > 0) {
        LOG.warn("custom persistence handlers and custom criteria not supported for update types other than BASIC");
    }
    PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
    Entity entity = persistencePackage.getEntity();
    AdornedTargetList adornedTargetList = (AdornedTargetList) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST);
    if (!adornedTargetList.getMutable()) {
        throw new SecurityServiceException("Field is not mutable");
    }
    try {
        AdornedTargetRetrieval adornedTargetRetrieval = new AdornedTargetRetrieval(persistencePackage, entity, adornedTargetList).invokeForUpdate();
        List<Serializable> records = adornedTargetRetrieval.getRecords();
        Assert.isTrue(!CollectionUtils.isEmpty(records), "Entity not found");
        int index = adornedTargetRetrieval.getIndex();
        Map<String, FieldMetadata> mergedProperties = adornedTargetRetrieval.getMergedProperties();
        FieldManager fieldManager = getFieldManager();
        Serializable myRecord;
        if (adornedTargetList.getSortField() != null && entity.findProperty(adornedTargetList.getSortField()).getValue() != null) {
            myRecord = records.get(index);
            BigDecimal requestedSequence = new BigDecimal(entity.findProperty(adornedTargetList.getSortField()).getValue());
            BigDecimal previousSequence = new BigDecimal(String.valueOf(getFieldManager().getFieldValue(myRecord, adornedTargetList.getSortField())));
            if (!previousSequence.equals(requestedSequence)) {
                // Sequence has changed. Rebalance the list
                myRecord = records.remove(index);
                myRecord = createPopulatedInstance(myRecord, entity, mergedProperties, false);
                if (CollectionUtils.isEmpty(records)) {
                    records.add(myRecord);
                } else {
                    records.add(requestedSequence.intValue() - 1, myRecord);
                }
                index = 1;
                Class<?> type = fieldManager.getField(myRecord.getClass(), adornedTargetList.getSortField()).getType();
                boolean isBigDecimal = BigDecimal.class.isAssignableFrom(type);
                for (Serializable record : records) {
                    fieldManager.setFieldValue(record, adornedTargetList.getSortField(), isBigDecimal ? new BigDecimal(index) : Long.valueOf(index));
                    index++;
                }
            }
        } else {
            myRecord = records.get(index);
        }
        String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
        Class<?>[] entities = persistenceManager.getPolymorphicEntities(ceilingEntityFullyQualifiedClassname);
        Map<String, FieldMetadata> mergedPropertiesTarget = persistenceManager.getDynamicEntityDao().getMergedProperties(ceilingEntityFullyQualifiedClassname, entities, null, persistencePerspective.getAdditionalNonPersistentProperties(), persistencePerspective.getAdditionalForeignKeys(), MergedPropertyType.PRIMARY, persistencePerspective.getPopulateToOneFields(), persistencePerspective.getIncludeFields(), persistencePerspective.getExcludeFields(), persistencePerspective.getConfigurationKey(), "");
        myRecord = createPopulatedInstance(myRecord, entity, mergedProperties, false);
        myRecord = persistenceManager.getDynamicEntityDao().merge(myRecord);
        List<Serializable> myList = new ArrayList<Serializable>();
        myList.add(myRecord);
        Entity[] payload = getRecords(mergedPropertiesTarget, myList, mergedProperties, adornedTargetList.getTargetObjectPath(), null);
        entity = payload[0];
        return entity;
    } catch (Exception e) {
        throw new ServiceException("Problem updating entity : " + e.getMessage(), e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) Serializable(java.io.Serializable) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) ServiceException(org.broadleafcommerce.common.exception.ServiceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) AdornedTargetList(org.broadleafcommerce.openadmin.dto.AdornedTargetList)

Example 12 with AdornedTargetList

use of org.broadleafcommerce.openadmin.dto.AdornedTargetList in project BroadleafCommerce by BroadleafCommerce.

the class AdornedTargetListPersistenceModule method fetch.

@Override
public DynamicResultSet fetch(PersistencePackage persistencePackage, CriteriaTransferObject cto) throws ServiceException {
    PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
    String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
    AdornedTargetList adornedTargetList = (AdornedTargetList) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST);
    Entity[] payload;
    int totalRecords;
    try {
        Class<?>[] entities = persistenceManager.getPolymorphicEntities(ceilingEntityFullyQualifiedClassname);
        Map<String, FieldMetadata> mergedPropertiesTarget = persistenceManager.getDynamicEntityDao().getMergedProperties(ceilingEntityFullyQualifiedClassname, entities, null, persistencePerspective.getAdditionalNonPersistentProperties(), persistencePerspective.getAdditionalForeignKeys(), MergedPropertyType.PRIMARY, persistencePerspective.getPopulateToOneFields(), persistencePerspective.getIncludeFields(), persistencePerspective.getExcludeFields(), persistencePerspective.getConfigurationKey(), "");
        AdornedTargetRetrieval adornedTargetRetrieval = new AdornedTargetRetrieval(persistencePackage, adornedTargetList, cto).invokeForFetch();
        List<Serializable> records = adornedTargetRetrieval.getRecords();
        Map<String, FieldMetadata> mergedProperties = adornedTargetRetrieval.getMergedProperties();
        payload = getRecords(mergedPropertiesTarget, records, mergedProperties, adornedTargetList.getTargetObjectPath(), persistencePackage.getCustomCriteria());
        totalRecords = getTotalRecords(adornedTargetList.getAdornedTargetEntityClassname(), adornedTargetRetrieval.getFilterMappings());
    } catch (Exception e) {
        throw new ServiceException("Unable to fetch results for " + adornedTargetList.getAdornedTargetEntityClassname(), e);
    }
    DynamicResultSet results = new DynamicResultSet(null, payload, totalRecords);
    return results;
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) Serializable(java.io.Serializable) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) ServiceException(org.broadleafcommerce.common.exception.ServiceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) AdornedTargetList(org.broadleafcommerce.openadmin.dto.AdornedTargetList) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet)

Example 13 with AdornedTargetList

use of org.broadleafcommerce.openadmin.dto.AdornedTargetList 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)

Example 14 with AdornedTargetList

use of org.broadleafcommerce.openadmin.dto.AdornedTargetList in project BroadleafCommerce by BroadleafCommerce.

the class AdminBasicEntityController method updateCollectionItemSequence.

/**
 * Updates the given collection item's sequence. This should only be triggered for adorned target collections
 * where a sort field is specified -- any other invocation is incorrect and will result in an exception.
 *
 * @param request
 * @param response
 * @param model
 * @param pathVars
 * @param id
 * @param collectionField
 * @param collectionItemId
 * @return an object explaining the state of the operation
 * @throws Exception
 */
@RequestMapping(value = "/{id}/{collectionField:.*}/{collectionItemId}/{alternateId}/sequence", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> updateCollectionItemSequence(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable(value = "id") String id, @PathVariable(value = "collectionField") String collectionField, @PathVariable(value = "collectionItemId") String collectionItemId, @RequestParam(value = "newSequence") String newSequence, @PathVariable(value = "alternateId") String alternateId) throws Exception {
    String sectionKey = getSectionKey(pathVars);
    String mainClassName = getClassNameForSection(sectionKey);
    List<SectionCrumb> sectionCrumbs = getSectionCrumbs(request, sectionKey, id);
    ClassMetadata mainMetadata = service.getClassMetadata(getSectionPersistencePackageRequest(mainClassName, sectionCrumbs, pathVars)).getDynamicResultSet().getClassMetaData();
    Property collectionProperty = mainMetadata.getPMap().get(collectionField);
    FieldMetadata md = collectionProperty.getMetadata();
    PersistencePackageRequest ppr = getSectionPersistencePackageRequest(mainClassName, sectionCrumbs, pathVars);
    ppr.addCustomCriteria("reorderParentEntityFetch");
    Entity parentEntity = service.getRecord(ppr, id, mainMetadata, false).getDynamicResultSet().getRecords()[0];
    ppr = PersistencePackageRequest.fromMetadata(md, sectionCrumbs);
    if (md instanceof AdornedTargetCollectionMetadata) {
        AdornedTargetCollectionMetadata fmd = (AdornedTargetCollectionMetadata) md;
        AdornedTargetList atl = ppr.getAdornedList();
        // Get an entity form for the entity
        EntityForm entityForm = formService.buildAdornedListForm(fmd, ppr.getAdornedList(), id, false, sectionCrumbs, false);
        Entity entity = service.getAdvancedCollectionRecord(mainMetadata, parentEntity, collectionProperty, collectionItemId, sectionCrumbs, alternateId, new String[] { "reorderChildEntityFetch" }).getDynamicResultSet().getRecords()[0];
        formService.populateEntityFormFields(entityForm, entity);
        formService.populateAdornedEntityFormFields(entityForm, entity, ppr.getAdornedList());
        // Set the new sequence (note that it will come in 0-indexed but the persistence module expects 1-indexed)
        int sequenceValue = Integer.parseInt(newSequence) + 1;
        Field field = entityForm.findField(atl.getSortField());
        field.setValue(String.valueOf(sequenceValue));
        Map<String, Object> responseMap = new HashMap<>();
        PersistenceResponse persistenceResponse = service.updateSubCollectionEntity(entityForm, mainMetadata, collectionProperty, parentEntity, collectionItemId, alternateId, sectionCrumbs);
        Property displayOrder = persistenceResponse.getEntity().findProperty(atl.getSortField());
        responseMap.put("status", "ok");
        responseMap.put("field", collectionField);
        responseMap.put("newDisplayOrder", displayOrder == null ? null : displayOrder.getValue());
        return responseMap;
    } else if (md instanceof BasicCollectionMetadata) {
        BasicCollectionMetadata cd = (BasicCollectionMetadata) md;
        Map<String, Object> responseMap = new HashMap<>();
        Entity entity = service.getRecord(ppr, collectionItemId, mainMetadata, false).getDynamicResultSet().getRecords()[0];
        ClassMetadata collectionMetadata = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
        EntityForm entityForm = formService.createEntityForm(collectionMetadata, sectionCrumbs);
        if (!StringUtils.isEmpty(cd.getSortProperty())) {
            Field f = new Field().withName(cd.getSortProperty()).withFieldType(SupportedFieldType.HIDDEN.toString());
            entityForm.addHiddenField(mainMetadata, f);
        }
        formService.populateEntityFormFields(entityForm, entity);
        if (!StringUtils.isEmpty(cd.getSortProperty())) {
            int sequenceValue = Integer.parseInt(newSequence) + 1;
            Field field = entityForm.findField(cd.getSortProperty());
            field.setValue(String.valueOf(sequenceValue));
        }
        PersistenceResponse persistenceResponse = service.updateSubCollectionEntity(entityForm, mainMetadata, collectionProperty, parentEntity, collectionItemId, sectionCrumbs);
        Property displayOrder = persistenceResponse.getEntity().findProperty(cd.getSortProperty());
        responseMap.put("status", "ok");
        responseMap.put("field", collectionField);
        responseMap.put("newDisplayOrder", displayOrder == null ? null : displayOrder.getValue());
        return responseMap;
    } else {
        throw new UnsupportedOperationException("Cannot handle sequencing for non adorned target collection fields.");
    }
}
Also used : ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) Entity(org.broadleafcommerce.openadmin.dto.Entity) EntityForm(org.broadleafcommerce.openadmin.web.form.entity.EntityForm) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) HashMap(java.util.HashMap) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) PersistenceResponse(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceResponse) SectionCrumb(org.broadleafcommerce.openadmin.dto.SectionCrumb) Field(org.broadleafcommerce.openadmin.web.form.entity.Field) BasicCollectionMetadata(org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata) Property(org.broadleafcommerce.openadmin.dto.Property) AdornedTargetList(org.broadleafcommerce.openadmin.dto.AdornedTargetList) Map(java.util.Map) HashMap(java.util.HashMap) FlashMap(org.springframework.web.servlet.FlashMap) MultiValueMap(org.springframework.util.MultiValueMap) AdornedTargetCollectionMetadata(org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

AdornedTargetList (org.broadleafcommerce.openadmin.dto.AdornedTargetList)14 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)8 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)8 Entity (org.broadleafcommerce.openadmin.dto.Entity)6 PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 SecurityServiceException (org.broadleafcommerce.common.exception.SecurityServiceException)5 ServiceException (org.broadleafcommerce.common.exception.ServiceException)5 AdornedTargetCollectionMetadata (org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata)5 Serializable (java.io.Serializable)4 ArrayList (java.util.ArrayList)4 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)4 FilterMapping (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping)4 BasicCollectionMetadata (org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata)3 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)3 Property (org.broadleafcommerce.openadmin.dto.Property)3 Field (java.lang.reflect.Field)2 BigDecimal (java.math.BigDecimal)2 List (java.util.List)2 ManyToOne (javax.persistence.ManyToOne)2