Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you 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.
 
 package org.apache.cxf.jaxrs.ext.search;
 
 import java.sql.Time;
 import java.util.Date;
 import java.util.Map;
 import java.util.Set;
 
 
 
 public abstract class AbstractSearchConditionParser<T> implements SearchConditionParser<T> {
     
     private static final Annotation[] EMPTY_ANNOTTAIONS = new Annotation[]{};
     protected final Map<StringStringcontextProperties;
     protected final Class<T> conditionClass;
     protected Beanspector<T> beanspector;       
     protected Map<StringStringbeanPropertiesMap;
         
     protected AbstractSearchConditionParser(Class<T> tclass) {
         this(tclass, Collections.<StringString>emptyMap(), null);
     }
     
     protected AbstractSearchConditionParser(Class<T> tclass
                                             Map<StringStringcontextProperties,
                                             Map<StringStringbeanProperties) {
         this. = tclass;
         this. = contextProperties == null 
             ? Collections.<StringString>emptyMap() : contextProperties;
          = SearchBean.class.isAssignableFrom(tclass) ? null : new Beanspector<T>(tclass);
         this. = beanProperties;
     }
     
     protected String getActualSetterName(String setter) {
         String beanPropertyName =  == null ? null : .get(setter);
         return beanPropertyName != null ? beanPropertyName : setter;
     }
     
     protected Boolean isDecodeQueryValues() {
         return PropertyUtils.isTrue(.get(.));
     }
     
     protected TypeInfo getTypeInfo(String setterString value
         throws SearchParseExceptionPropertyNotFoundException {
         
         String name = getSetter(setter);
         
         TypeInfo typeInfo = null;
         try {
             typeInfo =  != null ? .getAccessorTypeInfo(name
                     : new TypeInfo(String.classString.class);
         } catch (Exception e) {
             // continue
         }
         if (typeInfo == null && !MessageUtils.isTrue(.get(.))) {
             throw new PropertyNotFoundException(namevalue);
         }
         return typeInfo;
     }
     
     protected String getSetter(String setter) {
         int index = getDotIndex(setter);
         if (index != -1) {
             return setter.substring(0, index).toLowerCase();
         } else {
            return setter;
        }
    }
    
    protected Object parseType(String originalPropName
                             Object ownerBean
                             Object lastCastedValue
                             String setter
                             TypeInfo typeInfo
                             String valuethrows SearchParseException {
        Class<?> valueType = typeInfo.getTypeClass();
        boolean isCollection = InjectionUtils.isSupportedCollectionOrArray(valueType);
        Class<?> actualType = isCollection ? InjectionUtils.getActualType(typeInfo.getGenericType()) : valueType;
        
        int index = getDotIndex(setter);
        if (index == -1) {
            Object castedValue = value;
            if (Date.class.isAssignableFrom(valueType)) {
                castedValue = convertToDate(valueTypevalue);
            } else {
                boolean isPrimitive = InjectionUtils.isPrimitive(valueType);
                boolean isPrimitiveOrEnum = isPrimitive || valueType.isEnum();
                if (ownerBean == null || isPrimitiveOrEnum) {
                    try {
                        CollectionCheck collCheck = getCollectionCheck(originalPropNameisCollectionactualType);
                        if (collCheck == null) {
                            castedValue = InjectionUtils.convertStringToPrimitive(valueactualType);
                        } 
                        if (collCheck == null && isCollection) {
                            castedValue = getCollectionSingleton(valueTypecastedValue);
                        } else if (isCollection) {
                            typeInfo.setCollectionCheckInfo(new CollectionCheckInfo(collCheckcastedValue));
                            castedValue = getEmptyCollection(valueType);
                        }
                    } catch (Exception e) {
                        throw new SearchParseException("Cannot convert String value \"" + value
                                                     + "\" to a value of class " + valueType.getName(), e);
                    }
                } else {
                    Class<?> classType = isCollection ? valueType : value.getClass(); 
                    try {
                        Method setterM = valueType.getMethod("set" + getMethodNameSuffix(setter),
                                                             new Class[]{classType});
                        Object objectValue = !isCollection ? value : getCollectionSingleton(valueTypevalue);
                        setterM.invoke(ownerBeannew Object[]{objectValue});
                        castedValue = objectValue
                    } catch (Throwable ex) {
                        throw new SearchParseException("Cannot convert String value \"" + value
                                                       + "\" to a value of class " + valueType.getName(), ex);
                    }
                    
                }
            }
            if (lastCastedValue != null) {
                castedValue = lastCastedValue;
            }
            return castedValue;
        } else {
            String[] names = setter.split("\\.");
            try {
                String nextPart = getMethodNameSuffix(names[1]);
                Method getterM = actualType.getMethod("get" + nextPartnew Class[]{});   
                Class<?> returnType = getterM.getReturnType();
                boolean returnCollection = InjectionUtils.isSupportedCollectionOrArray(returnType);
                Class<?> actualReturnType = !returnCollection ? returnType 
                    : InjectionUtils.getActualType(getterM.getGenericReturnType());
                
                boolean isPrimitive = !returnCollection 
                    && InjectionUtils.isPrimitive(returnType) || returnType.isEnum();
                boolean lastTry = names.length == 2 
                    && (isPrimitive 
                        || 
                        Date.class.isAssignableFrom(returnType
                        || returnCollection
                        || paramConverterAvailable(returnType));
                
                Object valueObject = ownerBean != null ? ownerBean 
                    : actualType.isInterface() 
                    ? Proxy.newProxyInstance(this.getClass().getClassLoader(), 
                                             new Class[]{actualType}, 
                                             new InterfaceProxy())
                    : actualType.newInstance();
                Object nextObject;
                
                if (lastTry) {
                    if (!returnCollection) {
                        nextObject = isPrimitive ? InjectionUtils.convertStringToPrimitive(valuereturnType
                            : convertToDate(returnTypevalue);
                    } else {
                        CollectionCheck collCheck = getCollectionCheck(originalPropNametrueactualReturnType);
                        if (collCheck == null) {
                            nextObject = getCollectionSingleton(valueTypevalue);
                        } else {
                            typeInfo.setCollectionCheckInfo(new CollectionCheckInfo(collCheckvalue));
                            nextObject = getEmptyCollection(valueType);
                        }
                    }
                } else if (!returnCollection) {
                    nextObject = returnType.newInstance();
                } else {
                    nextObject = actualReturnType.newInstance();
                }
                Method setterM = actualType.getMethod("set" + nextPartnew Class[]{returnType});
                Object valueObjectValue = lastTry || !returnCollection 
                    ? nextObject : getCollectionSingleton(valueTypenextObject); 
                setterM.invoke(valueObjectnew Object[]{valueObjectValue});
                
                if (lastTry) {
                    lastCastedValue = lastCastedValue == null ? valueObject : lastCastedValue;
                    return isCollection ? getCollectionSingleton(valueTypelastCastedValue) : lastCastedValue;
                } else {
                    lastCastedValue = valueObject;
                }
                
                TypeInfo nextTypeInfo = new TypeInfo(valueObjectValue.getClass(), getterM.getGenericReturnType()); 
                Object response = parseType(originalPropName,
                                 nextObject
                                 lastCastedValue
                                 setter.substring(index + 1), 
                                 nextTypeInfo
                                 value);
                if (ownerBean == null) {
                    return isCollection ? getCollectionSingleton(valueTypelastCastedValue) : lastCastedValue;
                } else {
                    return response;
                }
            } catch (Throwable e) {
                throw new SearchParseException("Cannot convert String value \"" + value
                                               + "\" to a value of class " + valueType.getName(), e);
            }
        }
    }
    private boolean paramConverterAvailable(Class<?> pClass) {
        Message m = JAXRSUtils.getCurrentMessage();
        ServerProviderFactory pf = m == null ? null : ServerProviderFactory.getInstance(m);
        return pf != null && pf.createParameterHandler(pClasspClass) != null;
    }
    private CollectionCheck getCollectionCheck(String propNameboolean isCollectionClass<?> actualCls) {
        if (isCollection) {
            if (InjectionUtils.isPrimitive(actualCls)) {
                if (isCount(propName)) {
                    return .;
                }
            } else {
                return .;
            }
        }
        return null;
    }
    
    protected boolean isCount(String propName) {
        return false;
    }
    
    private Object getCollectionSingleton(Class<?> collectionClsObject value) {
        if (Set.class.isAssignableFrom(collectionCls)) {
            return Collections.singleton(value);
        } else {
            return Collections.singletonList(value);
        }
    }
    
    private Object getEmptyCollection(Class<?> collectionCls) {
        if (Set.class.isAssignableFrom(collectionCls)) {
            return Collections.emptySet();
        } else {
            return Collections.emptyList();
        }
    }
    
    private Object convertToDate(Class<?> valueTypeString valuethrows SearchParseException {
        
        Message m = JAXRSUtils.getCurrentMessage();
        Object obj = InjectionUtils.createFromParameterHandler(valuevalueTypevalueType
                                                               new Annotation[]{}, m);
        if (obj != null) {
            return obj;
        }
        
        try {
            if (Timestamp.class.isAssignableFrom(valueType)) {
                return convertToTimestamp(value);
            } else if (Time.class.isAssignableFrom(valueType)) {
                return convertToTime(value);
            } else {
                return convertToDefaultDate(value);
            }
        } catch (ParseException e) {
            // is that duration?
            try {
                Date now = new Date();
                DatatypeFactory.newInstance().newDuration(value).addTo(now);
                return now;
            } catch (DatatypeConfigurationException e1) {
                throw new SearchParseException(e1);
            } catch (IllegalArgumentException e1) {
                throw new SearchParseException("Can parse " + value + " neither as date nor duration"e);
            }
        }
    }
    
    private Timestamp convertToTimestamp(String valuethrows ParseException {
        Date date = convertToDefaultDate(value);
        return new Timestamp(date.getTime());
    }
    
    private Time convertToTime(String valuethrows ParseException {
        Date date = convertToDefaultDate(value);
        return new Time(date.getTime());
    }
    
    private Date convertToDefaultDate(String valuethrows ParseException {
        DateFormat df = SearchUtils.getDateFormat();
        String dateValue = value;
        if (SearchUtils.isTimeZoneSupported(.)) {
            // zone in XML is "+01:00" in Java is "+0100"; stripping semicolon
            int idx = value.lastIndexOf(':');
            if (idx != -1) {
                dateValue = value.substring(0, idx) + value.substring(idx + 1);
            }
        }
        return df.parse(dateValue);
    }
    
    private String getMethodNameSuffix(String name) {
        if (name.length() == 1) {
            return name.toUpperCase();
        } else {
            return Character.toUpperCase(name.charAt(0)) + name.substring(1);
        }
    }       
    private int getDotIndex(String setter) {
        return this. == SearchBean.class ? -1 : setter.indexOf(".");
    }    
New to GrepCode? Check out our FAQ X