Search in sources :

Example 51 with Entity

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

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

the class FormBuilderServiceImpl method populateDropdownToOneFields.

protected void populateDropdownToOneFields(EntityForm ef, ClassMetadata cmd) throws ServiceException {
    for (Property p : cmd.getProperties()) {
        if (p.getMetadata() instanceof BasicFieldMetadata) {
            BasicFieldMetadata fmd = (BasicFieldMetadata) p.getMetadata();
            if (LookupType.DROPDOWN.equals(fmd.getLookupType()) && !ArrayUtils.contains(getFormHiddenVisibilities(), fmd.getVisibility())) {
                // Get the records
                PersistencePackageRequest toOnePpr = PersistencePackageRequest.standard().withCeilingEntityClassname(fmd.getForeignKeyClass());
                Entity[] rows = adminEntityService.getRecords(toOnePpr).getDynamicResultSet().getRecords();
                // Determine the id field
                String idProp = null;
                ClassMetadata foreignClassMd = adminEntityService.getClassMetadata(toOnePpr).getDynamicResultSet().getClassMetaData();
                for (Property foreignP : foreignClassMd.getProperties()) {
                    if (foreignP.getMetadata() instanceof BasicFieldMetadata) {
                        BasicFieldMetadata foreignFmd = (BasicFieldMetadata) foreignP.getMetadata();
                        if (SupportedFieldType.ID.equals(foreignFmd.getFieldType())) {
                            idProp = foreignP.getName();
                        }
                    }
                }
                if (idProp == null) {
                    throw new RuntimeException("Could not determine ID property for " + fmd.getForeignKeyClass());
                }
                // Determine the display field
                String displayProp = fmd.getLookupDisplayProperty();
                // Build the options map
                Map<String, String> options = new HashMap<>();
                for (Entity row : rows) {
                    Property prop = row.findProperty(displayProp);
                    if (prop == null) {
                        LOG.warn("Could not find displayProp [" + StringUtil.sanitize(displayProp) + "] on entity [" + ef.getCeilingEntityClassname() + "]");
                    } else {
                        String displayValue = prop.getDisplayValue();
                        if (StringUtils.isBlank(displayValue)) {
                            displayValue = prop.getValue();
                        }
                        options.put(row.findProperty(idProp).getValue(), displayValue);
                    }
                }
                // Set the options on the entity field
                ComboField cf = (ComboField) ef.findField(p.getName());
                cf.setOptions(options);
            }
        }
    }
}
Also used : ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity) Entity(org.broadleafcommerce.openadmin.dto.Entity) ComboField(org.broadleafcommerce.openadmin.web.form.entity.ComboField) HashMap(java.util.HashMap) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) Property(org.broadleafcommerce.openadmin.dto.Property)

Example 53 with Entity

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

the class FormBuilderServiceImpl method buildMapForm.

@Override
public EntityForm buildMapForm(MapMetadata mapMd, final MapStructure mapStructure, ClassMetadata cmd, String parentId, EntityForm ef) throws ServiceException {
    ForeignKey foreignKey = (ForeignKey) mapMd.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY);
    ef.setEntityType(foreignKey.getForeignKeyClass());
    Field keyField;
    if (!mapMd.getForceFreeFormKeys()) {
        // We will use a combo field to render the key choices
        ComboField temp = new ComboField();
        temp.withName("key").withFieldType("combo_field").withFriendlyName(mapStructure.getKeyPropertyFriendlyName());
        if (mapMd.getKeys() != null) {
            // The keys can be explicitly set in the annotation...
            temp.setOptions(mapMd.getKeys());
        } else {
            // Or they could be based on a different entity
            PersistencePackageRequest ppr = PersistencePackageRequest.standard().withCeilingEntityClassname(mapMd.getMapKeyOptionEntityClass());
            DynamicResultSet drs = adminEntityService.getRecords(ppr).getDynamicResultSet();
            for (Entity entity : drs.getRecords()) {
                String keyValue = entity.getPMap().get(mapMd.getMapKeyOptionEntityValueField()).getValue();
                String keyDisplayValue = entity.getPMap().get(mapMd.getMapKeyOptionEntityDisplayField()).getValue();
                temp.putOption(keyValue, keyDisplayValue);
            }
        }
        keyField = temp;
    } else {
        keyField = new Field().withName("key").withFieldType(SupportedFieldType.STRING.toString()).withFriendlyName(mapStructure.getKeyPropertyFriendlyName());
    }
    keyField.setRequired(true);
    ef.addMapKeyField(cmd, keyField);
    // Set the fields for this form
    List<Property> mapFormProperties;
    if (mapMd.isSimpleValue()) {
        ef.setIdProperty("key");
        mapFormProperties = new ArrayList<>();
        Property valueProp = cmd.getPMap().get("value");
        mapFormProperties.add(valueProp);
    } else {
        String valueClassName = mapStructure.getValueClassName();
        List<String> classNames = getValueClassNames(valueClassName);
        mapFormProperties = new ArrayList<>(Arrays.asList(cmd.getProperties()));
        filterMapFormProperties(mapFormProperties, classNames);
    }
    setEntityFormFields(cmd, ef, mapFormProperties);
    Field f = new Field().withName("priorKey").withFieldType(SupportedFieldType.HIDDEN.toString());
    ef.addHiddenField(cmd, f);
    ef.setParentId(parentId);
    return ef;
}
Also used : CodeField(org.broadleafcommerce.openadmin.web.form.entity.CodeField) RuleBuilderField(org.broadleafcommerce.openadmin.web.form.component.RuleBuilderField) Field(org.broadleafcommerce.openadmin.web.form.entity.Field) ComboField(org.broadleafcommerce.openadmin.web.form.entity.ComboField) MediaField(org.broadleafcommerce.openadmin.web.form.component.MediaField) ComboField(org.broadleafcommerce.openadmin.web.form.entity.ComboField) AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity) Entity(org.broadleafcommerce.openadmin.dto.Entity) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) ForeignKey(org.broadleafcommerce.openadmin.dto.ForeignKey) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet) Property(org.broadleafcommerce.openadmin.dto.Property)

Example 54 with Entity

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

the class FormBuilderServiceImpl method createListGrid.

/**
 * Populate a ListGrid with ListGridRecords.
 *
 * @param className
 * @param headerFields
 * @param type
 * @param drs
 * @param sectionKey
 * @param order
 * @param idProperty
 * @param sectionCrumbs
 * @param sortPropery
 * @return
 */
protected ListGrid createListGrid(String className, List<Field> headerFields, ListGrid.Type type, DynamicResultSet drs, String sectionKey, Integer order, String idProperty, List<SectionCrumb> sectionCrumbs, String sortPropery) {
    // Create the list grid and set some basic attributes
    ListGrid listGrid = new ListGrid();
    listGrid.setClassName(className);
    listGrid.getHeaderFields().addAll(headerFields);
    listGrid.setListGridType(type);
    listGrid.setSectionCrumbs(sectionCrumbs);
    listGrid.setSectionKey(sectionKey);
    listGrid.setOrder(order);
    listGrid.setIdProperty(idProperty);
    listGrid.setStartIndex(drs.getStartIndex());
    listGrid.setTotalRecords(drs.getTotalRecords());
    listGrid.setPageSize(drs.getPageSize());
    String sectionIdentifier = extractSectionIdentifierFromCrumb(sectionCrumbs);
    AdminSection section = navigationService.findAdminSectionByClassAndSectionId(className, sectionIdentifier);
    if (section != null) {
        listGrid.setExternalEntitySectionKey(section.getUrl());
    }
    // format date list grid cells
    SimpleDateFormat formatter = new SimpleDateFormat("MMM d, Y @ hh:mma");
    DateFormatSymbols symbols = new DateFormatSymbols(Locale.getDefault());
    symbols.setAmPmStrings(new String[] { "am", "pm" });
    formatter.setDateFormatSymbols(symbols);
    // that are used for the header fields.
    for (Entity e : drs.getRecords()) {
        ListGridRecord record = new ListGridRecord();
        record.setListGrid(listGrid);
        record.setDirty(e.isDirty());
        record.setEntity(e);
        if (StringUtils.isNotBlank(sortPropery) && e.findProperty(sortPropery) != null) {
            Property sort = e.findProperty(sortPropery);
            record.setDisplayOrder(sort.getValue());
        }
        if (e.findProperty("hasError") != null) {
            Boolean hasError = Boolean.parseBoolean(e.findProperty("hasError").getValue());
            record.setIsError(hasError);
            if (hasError) {
                ExtensionResultStatusType messageResultStatus = listGridErrorExtensionManager.getProxy().determineErrorMessageForEntity(e, record);
                if (ExtensionResultStatusType.NOT_HANDLED.equals(messageResultStatus)) {
                    record.setErrorKey("listgrid.record.error");
                }
            }
        }
        if (e.findProperty("progressStatus") != null) {
            ExtensionResultStatusType messageResultStatus = listGridErrorExtensionManager.getProxy().determineStatusMessageForEntity(e, record);
            if (ExtensionResultStatusType.NOT_HANDLED.equals(messageResultStatus)) {
                record.setStatus(e.findProperty("progressStatus").getValue());
                record.setStatusCssClass("listgrid.record.status");
            }
        }
        if (e.findProperty(idProperty) != null) {
            record.setId(e.findProperty(idProperty).getValue());
        }
        for (Field headerField : headerFields) {
            Property p = e.findProperty(headerField.getName());
            if (p != null) {
                Field recordField = new Field().withName(headerField.getName()).withFriendlyName(headerField.getFriendlyName()).withOrder(p.getMetadata().getOrder());
                if (headerField instanceof ComboField) {
                    recordField.setValue(((ComboField) headerField).getOption(p.getValue()));
                    recordField.setDisplayValue(p.getDisplayValue());
                } else {
                    if (headerField.getFieldType().equals("DATE")) {
                        try {
                            Date date = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").parse(p.getValue());
                            String newValue = formatter.format(date);
                            recordField.setValue(newValue);
                        } catch (Exception ex) {
                            recordField.setValue(p.getValue());
                        }
                    } else {
                        recordField.setValue(p.getValue());
                    }
                    recordField.setDisplayValue(p.getDisplayValue());
                }
                recordField.setDerived(isDerivedField(headerField, recordField, p));
                record.getFields().add(recordField);
            }
        }
        if (e.findProperty(AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY) != null) {
            Field hiddenField = new Field().withName(AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY);
            hiddenField.setValue(e.findProperty(AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY).getValue());
            record.getHiddenFields().add(hiddenField);
        }
        if (e.findProperty(BasicPersistenceModule.ALTERNATE_ID_PROPERTY) != null) {
            record.setAltId(e.findProperty(BasicPersistenceModule.ALTERNATE_ID_PROPERTY).getValue());
        }
        extensionManager.getProxy().modifyListGridRecord(className, record, e);
        listGrid.getRecords().add(record);
    }
    if (drs.getFirstId() != null) {
        listGrid.setFirstId(drs.getFirstId());
    }
    if (drs.getLastId() != null) {
        listGrid.setLastId(drs.getLastId());
    }
    if (drs.getUpperCount() != null) {
        listGrid.setUpperCount(drs.getUpperCount());
    }
    if (drs.getLowerCount() != null) {
        listGrid.setLowerCount(drs.getLowerCount());
    }
    if (drs.getFetchType() != null) {
        listGrid.setFetchType(drs.getFetchType().toString());
    }
    if (drs.getTotalCountLessThanPageSize() != null) {
        listGrid.setTotalCountLessThanPageSize(drs.getTotalCountLessThanPageSize());
    }
    if (drs.getPromptSearch() != null) {
        listGrid.setPromptSearch(drs.getPromptSearch());
    }
    return listGrid;
}
Also used : AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity) Entity(org.broadleafcommerce.openadmin.dto.Entity) ListGridRecord(org.broadleafcommerce.openadmin.web.form.component.ListGridRecord) AdminSection(org.broadleafcommerce.openadmin.server.security.domain.AdminSection) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) ListGrid(org.broadleafcommerce.openadmin.web.form.component.ListGrid) Date(java.util.Date) ServiceException(org.broadleafcommerce.common.exception.ServiceException) IOException(java.io.IOException) NoSuchMessageException(org.springframework.context.NoSuchMessageException) ParseException(java.text.ParseException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) CodeField(org.broadleafcommerce.openadmin.web.form.entity.CodeField) RuleBuilderField(org.broadleafcommerce.openadmin.web.form.component.RuleBuilderField) Field(org.broadleafcommerce.openadmin.web.form.entity.Field) ComboField(org.broadleafcommerce.openadmin.web.form.entity.ComboField) MediaField(org.broadleafcommerce.openadmin.web.form.component.MediaField) ComboField(org.broadleafcommerce.openadmin.web.form.entity.ComboField) DateFormatSymbols(java.text.DateFormatSymbols) SimpleDateFormat(java.text.SimpleDateFormat) Property(org.broadleafcommerce.openadmin.dto.Property)

Example 55 with Entity

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

Aggregations

Entity (org.broadleafcommerce.openadmin.dto.Entity)112 Property (org.broadleafcommerce.openadmin.dto.Property)62 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)51 ServiceException (org.broadleafcommerce.common.exception.ServiceException)48 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)36 PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)34 ArrayList (java.util.ArrayList)25 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)24 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)24 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)20 Serializable (java.io.Serializable)19 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)19 SectionCrumb (org.broadleafcommerce.openadmin.dto.SectionCrumb)19 ValidationException (org.broadleafcommerce.openadmin.server.service.ValidationException)17 Map (java.util.Map)16 CriteriaTransferObject (org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)16 DataWrapper (org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataWrapper)16 HashMap (java.util.HashMap)15 SecurityServiceException (org.broadleafcommerce.common.exception.SecurityServiceException)14 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)14