blob: 8a9282b71c5171929d6280a3a554d2526c530216 [file] [log] [blame]
/**
* Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Florian Pirchner - Initial implementation
*/
package org.eclipse.osbp.runtime.web.vaadin.common.services.filter;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.osbp.runtime.common.annotations.TargetEnumConstraint;
import org.eclipse.osbp.runtime.common.annotations.TargetEnumConstraints;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.vaadin.data.Container.Filter;
import com.vaadin.data.util.filter.And;
import com.vaadin.data.util.filter.Compare;
import com.vaadin.data.util.filter.Not;
// TODO: Auto-generated Javadoc
/**
* Converts filter annotations to vaadin filters.
*/
public class AnnotationToVaadinFilterConverter {
/** The Constant LOGGER. */
private static final Logger LOGGER = LoggerFactory
.getLogger(AnnotationToVaadinFilterConverter.class);
/**
* Creates zero or more filters for the given annotation. If more filters
* are being created, they will be put into an And-Filter.<br>
* Returns <code>null</code> if no filter have been created.
*
* @param annotation
* the annotation
* @return the filter
*/
public static Filter createFilter(TargetEnumConstraints annotation) {
if (annotation != null) {
Set<Filter> filters = new HashSet<Filter>();
for (TargetEnumConstraint constraint : annotation.constraints()) {
Filter filter = createFilterFor(constraint);
if (filter != null) {
filters.add(filter);
}
}
if (filters.size() == 1) {
return filters.iterator().next();
} else if (filters.size() > 1) {
return new And(filters.toArray(new Filter[filters.size()]));
}
}
return null;
}
/**
* Creates one filter for the given annotation.
*
* @param constraint
* the constraint
* @return the filter
*/
public static Filter createFilterFor(TargetEnumConstraint constraint) {
if (constraint == null) {
return null;
}
Enum<?> enumLiteral = null;
try {
@SuppressWarnings("unchecked")
Class<Enum<?>> enumClass = (Class<Enum<?>>) constraint.enumClass();
for (Enum<?> enumx : enumClass.getEnumConstants()) {
if (enumx.name().equals(constraint.enumLiteral())) {
enumLiteral = enumx;
break;
}
}
} catch (Exception e) {
LOGGER.error("{}", e);
}
if (enumLiteral == null) {
return null;
}
Filter result = null;
switch (constraint.compareType()) {
case EQUALS:
result = new Compare.Equal(constraint.targetProperty(), enumLiteral);
break;
case GREATER:
result = new Compare.Greater(constraint.targetProperty(),
enumLiteral);
break;
case GREATER_EQ:
result = new Compare.GreaterOrEqual(constraint.targetProperty(),
enumLiteral);
break;
case LOWER:
result = new Compare.Less(constraint.targetProperty(), enumLiteral);
break;
case LOWER_EQ:
result = new Compare.LessOrEqual(constraint.targetProperty(),
enumLiteral);
break;
case NOT_EQ:
result = new Not(new Compare.Equal(constraint.targetProperty(),
enumLiteral));
break;
}
return result;
}
}