Search in sources :

Example 6 with AdminSection

use of org.broadleafcommerce.openadmin.server.security.domain.AdminSection in project BroadleafCommerce by BroadleafCommerce.

the class AdminAbstractController method getSectionKey.

// ***********************************************
// HELPER METHODS FOR SECTION-SPECIFIC OVERRIDES *
// ***********************************************
/**
 * This method is used to determine the current section key. For this default implementation, the sectionKey is pulled
 * from the pathVariable, {sectionKey}, as defined by the request mapping on this controller. To support controller
 * inheritance and allow more specialized controllers to delegate some methods to this basic controller, overridden
 * implementations of this method could return a hardcoded value instead of reading the map
 *
 * @param pathVars - the map of all currently bound path variables for this request
 * @return the sectionKey for this request
 */
protected String getSectionKey(Map<String, String> pathVars) {
    String sectionKey = pathVars.get("sectionKey");
    AdminSection typedEntitySection = null;
    HttpServletRequest request = BroadleafRequestContext.getBroadleafRequestContext().getRequest();
    if (request != null) {
        typedEntitySection = (AdminSection) request.getAttribute("typedEntitySection");
    }
    if (typedEntitySection != null) {
        sectionKey = typedEntitySection.getUrl().substring(1);
    }
    return sectionKey;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) AdminSection(org.broadleafcommerce.openadmin.server.security.domain.AdminSection)

Example 7 with AdminSection

use of org.broadleafcommerce.openadmin.server.security.domain.AdminSection 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 8 with AdminSection

use of org.broadleafcommerce.openadmin.server.security.domain.AdminSection in project BroadleafCommerce by BroadleafCommerce.

the class FormBuilderServiceImpl method populateEntityForm.

@Override
public void populateEntityForm(ClassMetadata cmd, EntityForm ef, List<SectionCrumb> sectionCrumbs) throws ServiceException {
    ef.setCeilingEntityClassname(cmd.getCeilingType());
    String sectionIdentifier = extractSectionIdentifierFromCrumb(sectionCrumbs);
    AdminSection section = navigationService.findAdminSectionByClassAndSectionId(cmd.getCeilingType(), sectionIdentifier);
    if (section != null) {
        ef.setSectionKey(section.getUrl());
    } else {
        ef.setSectionKey(cmd.getCeilingType());
    }
    ef.setSectionCrumbsImpl(sectionCrumbs);
    setEntityFormTabsAndGroups(ef, cmd.getTabAndGroupMetadata());
    setEntityFormFields(cmd, ef, Arrays.asList(cmd.getProperties()));
    populateDropdownToOneFields(ef, cmd);
    extensionManager.getProxy().modifyUnpopulatedEntityForm(ef);
}
Also used : AdminSection(org.broadleafcommerce.openadmin.server.security.domain.AdminSection)

Example 9 with AdminSection

use of org.broadleafcommerce.openadmin.server.security.domain.AdminSection in project BroadleafCommerce by BroadleafCommerce.

the class FormBuilderServiceImpl method constructFieldDTOFromFieldData.

protected FieldDTO constructFieldDTOFromFieldData(Field field, BasicFieldMetadata fmd) {
    FieldDTO fieldDTO = new FieldDTO();
    // translate the label to display
    String label = field.getFriendlyName();
    BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
    MessageSource messages = context.getMessageSource();
    if (messages != null) {
        label = messages.getMessage(label, null, label, context.getJavaLocale());
    }
    fieldDTO.setLabel(label);
    fieldDTO.setId(field.getName());
    if (field.getFieldType().equals("STRING")) {
        fieldDTO.setOperators("blcFilterOperators_Text");
    } else if (field.getFieldType().equals("DATE")) {
        fieldDTO.setOperators("blcFilterOperators_Date");
    } else if (field.getFieldType().equals("NUMBER") || field.getFieldType().equals("MONEY") || field.getFieldType().equals("DECIMAL")) {
        fieldDTO.setOperators("blcFilterOperators_Numeric");
    } else if (field.getFieldType().equals("BOOLEAN")) {
        fieldDTO.setOperators("blcFilterOperators_Boolean");
    } else if (field.getFieldType().equals("BROADLEAF_ENUMERATION")) {
        fieldDTO.setOperators("blcFilterOperators_Enumeration");
        fieldDTO.setInput("select");
        fieldDTO.setType("string");
        String[][] enumerationValues = fmd.getEnumerationValues();
        Map<String, String> enumMap = new HashMap<>();
        for (int i = 0; i < enumerationValues.length; i++) {
            enumMap.put(enumerationValues[i][0], enumerationValues[i][1]);
        }
        fieldDTO.setValues(new JSONObject(enumMap).toString());
    } else if (field.getFieldType().equals("ADDITIONAL_FOREIGN_KEY")) {
        fieldDTO.setOperators("blcFilterOperators_Selectize");
        fieldDTO.setType("string");
        AdminSection section = adminNavigationService.findAdminSectionByClassAndSectionId(fmd.getForeignKeyClass(), null);
        if (section != null) {
            String sectionKey = section.getUrl().substring(1);
            fieldDTO.setSelectizeSectionKey(sectionKey);
        } else {
            fieldDTO.setSelectizeSectionKey(fmd.getForeignKeyClass());
        }
    } else {
        fieldDTO.setOperators("blcFilterOperators_Text");
    }
    return fieldDTO;
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) HashMap(java.util.HashMap) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) AdminSection(org.broadleafcommerce.openadmin.server.security.domain.AdminSection) MessageSource(org.springframework.context.MessageSource) EntityFormModifierDataPoint(org.broadleafcommerce.openadmin.server.security.service.EntityFormModifierDataPoint) FieldDTO(org.broadleafcommerce.openadmin.web.rulebuilder.dto.FieldDTO)

Example 10 with AdminSection

use of org.broadleafcommerce.openadmin.server.security.domain.AdminSection in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafAdminTypedEntityRequestFilter method isRequestForTypedEntity.

@SuppressWarnings("unchecked")
public boolean isRequestForTypedEntity(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    String servletPath = request.getServletPath();
    if (!servletPath.contains(":")) {
        return false;
    }
    // Find the Admin Section for the typed entity.
    String sectionKey = getSectionKeyFromRequest(request);
    AdminSection typedEntitySection = adminNavigationService.findAdminSectionByURI(sectionKey);
    // If the Typed Entity Section does not exist, continue with the filter chain.
    if (typedEntitySection == null) {
        return false;
    }
    // Check if the item requested matches the item section
    TypedEntity typedEntity = getTypedEntityFromServletPathId(servletPath, typedEntitySection.getCeilingEntity());
    if (typedEntity != null && !typeMatchesAdminSection(typedEntity, sectionKey)) {
        String redirectUrl = getTypeAdminSectionMismatchUrl(typedEntity, typedEntitySection.getCeilingEntity(), request.getRequestURI(), sectionKey);
        response.sendRedirect(redirectUrl);
        return true;
    }
    // Check if admin user has access to this section.
    if (!adminUserHasAccess(typedEntitySection)) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN, "Access is denied");
        return true;
    }
    // Add the typed entity admin section to the request.
    request.setAttribute("typedEntitySection", typedEntitySection);
    // Find the type and build the new path.
    String type = getEntityTypeFromRequest(request);
    final String forwardPath = servletPath.replace(type, "");
    // Get the type field name on the Entity for the given section.
    String typedFieldName = getTypeFieldName(typedEntitySection);
    // Build out the new parameter map to be forwarded.
    final Map parameters = new LinkedHashMap(request.getParameterMap());
    if (typedFieldName != null) {
        parameters.put(typedFieldName, new String[] { type.substring(1).toUpperCase() });
    }
    // Build our request wrapper.
    final HttpServletRequestWrapper wrapper = new HttpServletRequestWrapper(request) {

        @Override
        public String getParameter(String name) {
            String[] response = (String[]) parameters.get(name);
            if (ArrayUtils.isEmpty(response)) {
                return null;
            } else {
                return response[0];
            }
        }

        @Override
        public Map getParameterMap() {
            return parameters;
        }

        @Override
        public Enumeration getParameterNames() {
            return new IteratorEnumeration(parameters.keySet().iterator());
        }

        @Override
        public String[] getParameterValues(String name) {
            return (String[]) parameters.get(name);
        }

        @Override
        public String getServletPath() {
            return forwardPath;
        }
    };
    requestProcessor.process(new ServletWebRequest(wrapper, response));
    // Forward the wrapper to the appropriate path
    wrapper.getRequestDispatcher(wrapper.getServletPath()).forward(wrapper, response);
    return true;
}
Also used : HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) IteratorEnumeration(org.apache.commons.collections4.iterators.IteratorEnumeration) AdminSection(org.broadleafcommerce.openadmin.server.security.domain.AdminSection) TypedEntity(org.broadleafcommerce.common.admin.domain.TypedEntity) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

AdminSection (org.broadleafcommerce.openadmin.server.security.domain.AdminSection)17 HashMap (java.util.HashMap)3 Query (javax.persistence.Query)3 TypedQuery (javax.persistence.TypedQuery)3 NoResultException (javax.persistence.NoResultException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 BroadleafRequestContext (org.broadleafcommerce.common.web.BroadleafRequestContext)2 Property (org.broadleafcommerce.openadmin.dto.Property)2 AdminModule (org.broadleafcommerce.openadmin.server.security.domain.AdminModule)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)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 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 HttpServletRequestWrapper (javax.servlet.http.HttpServletRequestWrapper)1 BeanComparator (org.apache.commons.beanutils.BeanComparator)1