org.broadleafcommerce.common.web.processor.DataDrivenEnumerationProcessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of broadleaf-common Show documentation
Show all versions of broadleaf-common Show documentation
A collection of classes shared by broadleaf profile, cms, admin, and core.
/*
* #%L
* BroadleafCommerce Common Libraries
* %%
* Copyright (C) 2009 - 2013 Broadleaf Commerce
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package org.broadleafcommerce.common.web.processor;
import org.apache.commons.lang3.StringUtils;
import org.broadleafcommerce.common.enumeration.domain.DataDrivenEnumeration;
import org.broadleafcommerce.common.enumeration.domain.DataDrivenEnumerationValue;
import org.broadleafcommerce.common.enumeration.service.DataDrivenEnumerationService;
import org.broadleafcommerce.common.web.dialect.AbstractModelVariableModifierProcessor;
import org.thymeleaf.Arguments;
import org.thymeleaf.dom.Element;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.annotation.Resource;
/**
* Processor that adds a list of {@link DataDriveEnumerationValue}s onto the model for a particular key.
* This will add a new variable on the model called 'enumValues'
*
* @param key (required) key for the {@link DataDrivenEnumeration} that the {@link DataDrivenEnumerationValue}s should be
* apart of. This corresponds to {@link DataDrivenEnumeration#getKey()}.
*
* @param sort (optional) ASCENDING or DESCENDING if the resulting values should be sorted by not. The sort will be on
* {@link DataDrivenEnumerationValue#getDisplay()}
*
* @author Phillip Verheyden (phillipuniverse)
*/
public class DataDrivenEnumerationProcessor extends AbstractModelVariableModifierProcessor {
@Resource(name = "blDataDrivenEnumerationService")
protected DataDrivenEnumerationService enumService;
/**
* @param elementName
*/
public DataDrivenEnumerationProcessor() {
super("enumeration");
}
@Override
protected void modifyModelAttributes(Arguments arguments, Element element) {
String key = element.getAttributeValue("key");
if (StringUtils.isEmpty(key)) {
throw new IllegalArgumentException("No 'key' parameter was passed to find enumeration values");
}
DataDrivenEnumeration ddEnum = enumService.findEnumByKey(key);
if (ddEnum == null) {
throw new IllegalArgumentException("Could not find a data driven enumeration keyed by " + key);
}
List enumValues = new ArrayList(ddEnum.getEnumValues());
final String sort = element.getAttributeValue("sort");
if (StringUtils.isNotEmpty(sort)) {
Collections.sort(enumValues, new Comparator() {
@Override
public int compare(DataDrivenEnumerationValue arg0, DataDrivenEnumerationValue arg1) {
if (sort.equals("ASCENDING")) {
return arg0.getDisplay().compareTo(arg1.getDisplay());
} else {
return arg1.getDisplay().compareTo(arg0.getDisplay());
}
}
});
}
addToModel(arguments, "enumValues", enumValues);
}
@Override
public int getPrecedence() {
return 1;
}
}