Search in sources :

Example 1 with ListGridRecord

use of org.broadleafcommerce.openadmin.web.form.component.ListGridRecord 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 2 with ListGridRecord

use of org.broadleafcommerce.openadmin.web.form.component.ListGridRecord in project BroadleafCommerce by BroadleafCommerce.

the class TranslationFormBuilderServiceImpl method buildListGrid.

@Override
public ListGrid buildListGrid(List<Translation> translations, boolean isRte) {
    // Set up the two header fields we're interested in for the translations list grid
    List<Field> headerFields = new ArrayList<Field>();
    headerFields.add(new Field().withName("localeCode").withFriendlyName("Translation_localeCode").withOrder(0));
    headerFields.add(new Field().withName("translatedValue").withFriendlyName("Translation_translatedValue").withOrder(10));
    // Create the list grid and set its basic properties
    ListGrid listGrid = new ListGrid();
    listGrid.getHeaderFields().addAll(headerFields);
    listGrid.setListGridType(ListGrid.Type.TRANSLATION);
    listGrid.setSelectType(ListGrid.SelectType.SINGLE_SELECT);
    listGrid.setCanFilterAndSort(false);
    // Allow add/update/remove actions, but provisioned especially for translation. Because of this, we will clone
    // the default actions so that we may change the class
    ListGridAction addAction = DefaultListGridActions.ADD.clone();
    ListGridAction removeAction = DefaultListGridActions.REMOVE.clone();
    ListGridAction updateAction = DefaultListGridActions.UPDATE.clone();
    addAction.setButtonClass("translation-grid-add");
    removeAction.setButtonClass("translation-grid-remove");
    updateAction.setButtonClass("translation-grid-update");
    listGrid.addToolbarAction(addAction);
    listGrid.addRowAction(updateAction);
    listGrid.addRowAction(removeAction);
    // TODO rework code elsewhere so these don't have to be added
    listGrid.setSectionKey(Translation.class.getCanonicalName());
    listGrid.setSubCollectionFieldName("translation");
    // Create records for each of the entries in the translations list
    for (Translation t : translations) {
        ListGridRecord record = new ListGridRecord();
        record.setListGrid(listGrid);
        record.setId(String.valueOf(t.getId()));
        Locale locale = localeService.findLocaleByCode(t.getLocaleCode());
        record.getFields().add(new Field().withName("localeCode").withFriendlyName("Translation_localeCode").withOrder(0).withValue(locale.getLocaleCode()).withDisplayValue(locale.getFriendlyName()));
        record.getFields().add(new Field().withName("translatedValue").withFriendlyName("Translation_translatedValue").withOrder(10).withValue(t.getTranslatedValue()).withDisplayValue(isRte ? getLocalizedEditToViewMessage() : t.getTranslatedValue()));
        listGrid.getRecords().add(record);
    }
    listGrid.setTotalRecords(listGrid.getRecords().size());
    return listGrid;
}
Also used : ListGridAction(org.broadleafcommerce.openadmin.web.form.component.ListGridAction) Locale(org.broadleafcommerce.common.locale.domain.Locale) ComboField(org.broadleafcommerce.openadmin.web.form.entity.ComboField) Field(org.broadleafcommerce.openadmin.web.form.entity.Field) Translation(org.broadleafcommerce.common.i18n.domain.Translation) ListGridRecord(org.broadleafcommerce.openadmin.web.form.component.ListGridRecord) ArrayList(java.util.ArrayList) ListGrid(org.broadleafcommerce.openadmin.web.form.component.ListGrid)

Example 3 with ListGridRecord

use of org.broadleafcommerce.openadmin.web.form.component.ListGridRecord in project BroadleafCommerce by BroadleafCommerce.

the class AssetFormBuilderServiceImpl method addImageThumbnailField.

@Override
public void addImageThumbnailField(ListGrid listGrid, String urlField) {
    listGrid.getHeaderFields().add(new Field().withName("thumbnail").withFriendlyName("Asset_thumbnail").withFieldType(SupportedFieldType.STRING.toString()).withOrder(Integer.MIN_VALUE).withColumnWidth("50px").withFilterSortDisabled(true));
    for (ListGridRecord record : listGrid.getRecords()) {
        // Get the value of the URL
        String imageUrl = record.getField(urlField).getValue();
        // Prepend the static asset url prefix if necessary
        String staticAssetUrlPrefix = staticAssetService.getStaticAssetUrlPrefix();
        if (staticAssetUrlPrefix != null && !staticAssetUrlPrefix.startsWith("/")) {
            staticAssetUrlPrefix = "/" + staticAssetUrlPrefix;
        }
        if (staticAssetUrlPrefix == null) {
            staticAssetUrlPrefix = "";
        } else {
            imageUrl = staticAssetUrlPrefix + imageUrl;
        }
        MediaField mf = (MediaField) new MediaField().withName("thumbnail").withFriendlyName("Asset_thumbnail").withFieldType(SupportedFieldType.IMAGE.toString()).withOrder(Integer.MIN_VALUE).withValue(imageUrl);
        // Add a hidden field for the large thumbnail path
        record.getHiddenFields().add(new Field().withName("cmsUrlPrefix").withValue(staticAssetUrlPrefix));
        record.getHiddenFields().add(new Field().withName("thumbnailKey").withValue("?smallAdminThumbnail"));
        record.getHiddenFields().add(new Field().withName("servletContext").withValue(BroadleafRequestContext.getBroadleafRequestContext().getRequest().getContextPath()));
        // Set the height value on this field
        mf.setHeight(operationMap.getNamedOperations().get("smallAdminThumbnail").get("resize-height-amount"));
        record.getFields().add(mf);
        // Since we've added a new field, we need to clear the cached map to ensure it will display
        record.clearFieldMap();
    }
}
Also used : MediaField(org.broadleafcommerce.openadmin.web.form.component.MediaField) Field(org.broadleafcommerce.openadmin.web.form.entity.Field) ListGridRecord(org.broadleafcommerce.openadmin.web.form.component.ListGridRecord) MediaField(org.broadleafcommerce.openadmin.web.form.component.MediaField)

Aggregations

ListGridRecord (org.broadleafcommerce.openadmin.web.form.component.ListGridRecord)3 Field (org.broadleafcommerce.openadmin.web.form.entity.Field)3 ListGrid (org.broadleafcommerce.openadmin.web.form.component.ListGrid)2 MediaField (org.broadleafcommerce.openadmin.web.form.component.MediaField)2 ComboField (org.broadleafcommerce.openadmin.web.form.entity.ComboField)2 IOException (java.io.IOException)1 DateFormatSymbols (java.text.DateFormatSymbols)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)1 SecurityServiceException (org.broadleafcommerce.common.exception.SecurityServiceException)1 ServiceException (org.broadleafcommerce.common.exception.ServiceException)1 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)1 Translation (org.broadleafcommerce.common.i18n.domain.Translation)1 Locale (org.broadleafcommerce.common.locale.domain.Locale)1 Entity (org.broadleafcommerce.openadmin.dto.Entity)1 Property (org.broadleafcommerce.openadmin.dto.Property)1 AdminSection (org.broadleafcommerce.openadmin.server.security.domain.AdminSection)1