Search in sources :

Example 21 with PersistencePerspective

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

the class CustomerCustomPersistenceHandler method add.

@Override
public Entity add(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    try {
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        Customer adminInstance = (Customer) Class.forName(entity.getType()[0]).newInstance();
        adminInstance.setId(customerService.findNextCustomerId());
        Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Customer.class.getName(), persistencePerspective);
        adminInstance = (Customer) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
        if (useEmailForLogin) {
            adminInstance.setUsername(adminInstance.getEmailAddress());
        }
        if (!entity.isPreAdd()) {
            Entity errorEntity = validateUniqueUsername(entity, adminInstance);
            if (errorEntity != null) {
                return errorEntity;
            }
        }
        adminInstance = dynamicEntityDao.merge(adminInstance);
        customerService.createRegisteredCustomerRoles(adminInstance);
        Entity adminEntity = helper.getRecord(adminProperties, adminInstance, null, null);
        return adminEntity;
    } catch (Exception e) {
        LOG.error("Unable to execute persistence activity", e);
        throw new ServiceException("Unable to add entity for " + entity.getType()[0], e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) Customer(org.broadleafcommerce.profile.core.domain.Customer) ServiceException(org.broadleafcommerce.common.exception.ServiceException)

Example 22 with PersistencePerspective

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

the class CustomerCustomPersistenceHandler method update.

@Override
public Entity update(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    try {
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Customer.class.getName(), persistencePerspective);
        Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
        Customer adminInstance = (Customer) dynamicEntityDao.retrieve(Class.forName(entity.getType()[0]), primaryKey);
        String passwordBefore = adminInstance.getPassword();
        adminInstance.setPassword(null);
        adminInstance = (Customer) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
        adminInstance.setPassword(passwordBefore);
        if (useEmailForLogin) {
            adminInstance.setUsername(adminInstance.getEmailAddress());
        }
        adminInstance = customerService.saveCustomer(adminInstance);
        Entity adminEntity = helper.getRecord(adminProperties, adminInstance, null, null);
        return adminEntity;
    } catch (Exception e) {
        throw new ServiceException("Unable to update entity for " + entity.getType()[0], e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) Customer(org.broadleafcommerce.profile.core.domain.Customer) ServiceException(org.broadleafcommerce.common.exception.ServiceException)

Example 23 with PersistencePerspective

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

the class IndexFieldCustomPersistenceHandler method update.

@Override
public Entity update(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    try {
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(IndexField.class.getName(), persistencePerspective);
        Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
        IndexField adminInstance = (IndexField) dynamicEntityDao.retrieve(Class.forName(entity.getType()[0]), primaryKey);
        return getEntity(persistencePackage, dynamicEntityDao, helper, entity, adminProperties, adminInstance);
    } catch (Exception e) {
        throw new ServiceException("Unable to perform update for entity: " + IndexField.class.getName(), e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject) IndexField(org.broadleafcommerce.core.search.domain.IndexField) ServiceException(org.broadleafcommerce.common.exception.ServiceException)

Example 24 with PersistencePerspective

use of org.broadleafcommerce.openadmin.dto.PersistencePerspective 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 25 with PersistencePerspective

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

Aggregations

PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)51 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)44 ServiceException (org.broadleafcommerce.common.exception.ServiceException)39 Entity (org.broadleafcommerce.openadmin.dto.Entity)34 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)27 Serializable (java.io.Serializable)16 InvocationTargetException (java.lang.reflect.InvocationTargetException)16 SecurityServiceException (org.broadleafcommerce.common.exception.SecurityServiceException)16 CriteriaTransferObject (org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)16 ForeignKey (org.broadleafcommerce.openadmin.dto.ForeignKey)14 Map (java.util.Map)10 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)9 ValidationException (org.broadleafcommerce.openadmin.server.service.ValidationException)9 FilterMapping (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping)7 HashMap (java.util.HashMap)6 AdornedTargetList (org.broadleafcommerce.openadmin.dto.AdornedTargetList)6 MapStructure (org.broadleafcommerce.openadmin.dto.MapStructure)6 SimpleValueMapStructure (org.broadleafcommerce.openadmin.dto.SimpleValueMapStructure)6 ParentEntityPersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException)6 PersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException)6