Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014-2015 the original author or authors.
   *
   * 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.
  */
 package org.dbflute.s2dao.metadata;
 
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 

Author(s):
modified by jflute (originated in S2Dao)
 
 public class TnProcedureMetaDataFactory {
 
     // ===================================================================================
     //                                                                           Attribute
     //                                                                           =========
 
     // ===================================================================================
     //                                                                                Main
     //                                                                                ====
     public TnProcedureMetaData createProcedureMetaData(String procedureNameClass<?> pmbType) {
         final TnProcedureMetaData procedureMetaData = new TnProcedureMetaData(procedureName);
         if (pmbType == null) {
             return procedureMetaData;
         } else {
             if (!isDtoType(pmbType)) {
                 throw new IllegalStateException("The pmb type was Not DTO type: " + pmbType.getName());
             }
         }
         final DfBeanDesc pmbDesc = DfBeanDescFactory.getBeanDesc(pmbType);
         final List<StringproppertyNameList = pmbDesc.getProppertyNameList();
         for (String propertyName : proppertyNameList) {
             final DfPropertyDesc parameterDesc = pmbDesc.getPropertyDesc(propertyName);
             if (parameterDesc.isReadable() && parameterDesc.isWritable()) {
                 registerParameterType(procedureMetaDataparameterDesc);
             }
         }
         procedureMetaData.fix();
         return procedureMetaData;
     }
 
     protected void registerParameterType(TnProcedureMetaData procedureMetaDataDfPropertyDesc parameterDesc) {
         final TnProcedureParameterType ppt = getProcedureParameterType(parameterDesc);
         if (ppt == null) {
             return;
         }
         procedureMetaData.addParameterType(ppt);
     }
 
         final String specificationExp = .getParameterSpecification(parameterDesc);
         if (specificationExp == null) {
             return null;
         }
         final TnProcedureParameterSpec spec = parseParameterSpec(specificationExpparameterDesc);
         final String type = spec.getParameterType();
         final TnProcedureParameterType ppt = createProcedureParameterType(parameterDesc);
         if (type.equalsIgnoreCase("in")) {
             ppt.setInType(true);
         } else if (type.equalsIgnoreCase("out")) {
             ppt.setOutType(true);
         } else if (type.equalsIgnoreCase("inout")) {
             ppt.setInType(true);
             ppt.setOutType(true);
         } else if (type.equalsIgnoreCase("return")) {
             ppt.setOutType(true);
             ppt.setReturnType(true);
        } else if (type.equalsIgnoreCase("notParamResult")) {
            ppt.setNotParamResultType(true);
        } else {
            String msg = "The parameter type should be 'in, out, inout, return, notParamResult':";
            msg = msg + " type=" + type;
            msg = msg + " class=" + DfTypeUtil.toClassTitle(parameterDesc.getBeanDesc().getBeanClass());
            msg = msg + " property=" + parameterDesc.getPropertyName();
            throw new IllegalStateException(msg);
        }
        ppt.setParameterOrder(spec.getParameterOrder());
        ppt.setValueType(findValueType(parameterDesc));
        return ppt;
    }
    protected TnProcedureParameterType createProcedureParameterType(final DfPropertyDesc parameterDesc) {
        final Type genericReturnType = parameterDesc.getReadMethod().getGenericReturnType();
        final Class<?> elementType = DfReflectionUtil.getGenericFirstClass(genericReturnType);
        return new TnProcedureParameterType(new TnProcedureParameterAccessor() {
            public Object getValue(Object target) {
                return parameterDesc.getValue(target);
            }
            public void setValue(Object targetObject value) {
                parameterDesc.setValue(targetvalue);
            }
        }, parameterDesc.getPropertyName(), parameterDesc.getPropertyType(), elementType);
    }
    // ===================================================================================
    //                                                             Parameter Specification
    //                                                             =======================
    protected TnProcedureParameterSpec parseParameterSpec(String specExpDfPropertyDesc parameterDesc) {
        final List<Stringlist = Srl.splitListTrimmed(specExp",");
        if (list.size() != 2) {
            final ExceptionMessageBuilder br = new ExceptionMessageBuilder();
            br.addNotice("The size of parameter spec elements was illegal.");
            br.addItem("ProcedurePmb");
            br.addElement(DfTypeUtil.toClassTitle(parameterDesc.getBeanDesc().getBeanClass()));
            br.addItem("Parameter");
            br.addElement(parameterDesc.getPropertyName());
            br.addElement(parameterDesc.getPropertyType());
            br.addItem("Parameter Spec");
            br.addElement(specExp);
            final String msg = br.buildExceptionMessage();
            throw new IllegalStateException(msg);
        }
        final String parameterType = list.get(0);
        final String parameterIndex = list.get(1);
        final TnProcedureParameterSpec spec = new TnProcedureParameterSpec();
        spec.setParameterType(parameterType);
        try {
            spec.setParameterOrder(DfTypeUtil.toInteger(parameterIndex));
        } catch (NumberFormatException e) {
            final ExceptionMessageBuilder br = new ExceptionMessageBuilder();
            br.addNotice("Failed to parse the parameter index as Integer.");
            br.addItem("ProcedurePmb");
            br.addElement(DfTypeUtil.toClassTitle(parameterDesc.getBeanDesc().getBeanClass()));
            br.addItem("Parameter");
            br.addElement(parameterDesc.getPropertyName());
            br.addElement(parameterDesc.getPropertyType());
            br.addItem("Parameter Spec");
            br.addElement(specExp);
            final String msg = br.buildExceptionMessage();
            throw new IllegalStateException(msg);
        }
        return spec;
    }
    protected static class TnProcedureParameterSpec {
        protected String _parameterType;
        protected Integer _parameterOrder;
        public String getParameterType() {
            return ;
        }
        public void setParameterType(String parameterType) {
            this. = parameterType;
        }
        public Integer getParameterOrder() {
            return ;
        }
        public void setParameterOrder(Integer parameterOrder) {
            this. = parameterOrder;
        }
    }
    // ===================================================================================
    //                                                                          Value Type
    //                                                                          ==========
    protected ValueType findValueType(DfPropertyDesc parameterDesc) {
        final Class<?> pmbType = parameterDesc.getBeanDesc().getBeanClass();
        final String paramName = parameterDesc.getPropertyName();
        final Class<?> paramType = parameterDesc.getPropertyType();
        final Object valueTypeDef = .getValueType(parameterDesc);
        if (valueTypeDef instanceof ValueType) {
            return (ValueTypevalueTypeDef;
        } else {
            final String keyName = (valueTypeDef != null ? valueTypeDef.toString() : null);
            final DBDef dbdef = ResourceContext.currentDBDef();
            return .provide(pmbTypeparamNameparamTypekeyNamedbdef);
        }
    }
    // ===================================================================================
    //                                                                Determination Helper
    //                                                                ====================
    protected boolean isCurrentDBDef(DBDef currentDBDef) {
        return ResourceContext.isCurrentDBDef(currentDBDef);
    }
    protected boolean isInstanceField(Field field) {
        final int mod = field.getModifiers();
        return !Modifier.isStatic(mod) && !Modifier.isFinal(mod);
    }
    protected boolean isDtoType(Class<?> clazz) {
        return !isSimpleType(clazz) && !isContainerType(clazz);
    }
    protected boolean isSimpleType(Class<?> clazz) {
        if (clazz == null) {
            throw new NullPointerException("clazz");
        }
        return clazz == String.class || clazz.isPrimitive() || clazz == Boolean.class || clazz == Character.class
                || Number.class.isAssignableFrom(clazz) || Date.class.isAssignableFrom(clazz) || Calendar.class.isAssignableFrom(clazz)
                || clazz == byte[].class;
    }
    protected boolean isContainerType(Class<?> clazz) {
        if (clazz == null) {
            throw new NullPointerException("clazz");
        }
        return Collection.class.isAssignableFrom(clazz) || Map.class.isAssignableFrom(clazz) || clazz.isArray();
    }
    // ===================================================================================
    //                                                                   Annotation Reader
    //                                                                   =================
    protected static class TnFieldProcedureAnnotationReader {
        protected static final String PARAMETER_SUFFIX = "_PROCEDURE_PARAMETER";
        protected static final String VALUE_TYPE_SUFFIX = "_VALUE_TYPE";

        
Get the specification of parameter.

Parameters:
propertyDesc The description of property. (NotNull)
Returns:
The specification of parameter. (NullAllowed)
        public String getParameterSpecification(DfPropertyDesc propertyDesc) {
            final String propertyName = propertyDesc.getPropertyName();
            final String annotationName = propertyName + ;
            final DfBeanDesc pmbDesc = propertyDesc.getBeanDesc();
            if (pmbDesc.hasField(annotationName)) {
                final Field field = pmbDesc.getField(annotationName);
                return (String) DfReflectionUtil.getValue(fieldnull);
            } else {
                return null;
            }
        }

        
Get the name or instance of value type for parameter.

Parameters:
propertyDesc The description of property. (NotNull)
Returns:
The string or instance of value type. (NullAllowed)
        public Object getValueType(DfPropertyDesc propertyDesc) {
            final String propertyName = propertyDesc.getPropertyName();
            final String annotationName = propertyName + ;
            final DfBeanDesc pmbDesc = propertyDesc.getBeanDesc();
            if (pmbDesc.hasField(annotationName)) {
                final Field field = pmbDesc.getField(annotationName);
                return DfReflectionUtil.getValue(fieldnull);
            } else {
                return null;
            }
        }
        protected Object getValue(Field fieldObject target) {
            try {
                return field.get(target);
            } catch (IllegalAccessException e) {
                String msg = "The getting of the field threw the exception:";
                msg = msg + " class=" + DfTypeUtil.toClassTitle(field.getDeclaringClass());
                msg = msg + " field=" + field.getName();
                throw new IllegalStateException(msge);
            }
        }
    }
New to GrepCode? Check out our FAQ X