/*
 * 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 com.libreecm.commonAPI.model.objects.properties;

import com.libreecm.commonAPI.model.objects.ObjectType;
/**
 * Helper Class for extending PropertyDefinition, ObjectType and get QueryName from it's id
 * 
 * @author org.chemistry.opencmis
 *
 */
public final class TypeUtil {

    private TypeUtil() {
    }

    public static PropertyDefinition<?> clonePropertyDefinition(PropertyDefinition<?> pd) {
        if (pd instanceof PropertyBooleanDefinitionImpl) {
            PropertyBooleanDefinitionImpl pdBoolDef = new PropertyBooleanDefinitionImpl();
            PropertyBooleanDefinitionImpl pdSrc = (PropertyBooleanDefinitionImpl) pd;
            initializePropertyDefinition(pd, pdBoolDef);
            pdBoolDef.setChoices(pdSrc.getChoices());
            pdBoolDef.setDefaultValue(pdSrc.getDefaultValue());
            return pdBoolDef;
        } else if (pd instanceof PropertyDateTimeDefinitionImpl) {
            PropertyDateTimeDefinitionImpl pdDateDef = new PropertyDateTimeDefinitionImpl();
            PropertyDateTimeDefinitionImpl pdSrc = (PropertyDateTimeDefinitionImpl) pd;
            initializePropertyDefinition(pd, pdDateDef);
            pdDateDef.setChoices(pdSrc.getChoices());
            pdDateDef.setDefaultValue(pdSrc.getDefaultValue());
            pdDateDef.setDateTimeResolution(pdSrc.getDateTimeResolution());
            return pdDateDef;
        } else if (pd instanceof PropertyDecimalDefinitionImpl) {
            PropertyDecimalDefinitionImpl pdDecDef = new PropertyDecimalDefinitionImpl();
            PropertyDecimalDefinitionImpl pdSrc = (PropertyDecimalDefinitionImpl) pd;
            initializePropertyDefinition(pd, pdDecDef);
            pdDecDef.setChoices(pdSrc.getChoices());
            pdDecDef.setDefaultValue(pdSrc.getDefaultValue());
            pdDecDef.setMinValue(pdSrc.getMinValue());
            pdDecDef.setMaxValue(pdSrc.getMaxValue());
            pdDecDef.setPrecision(pdSrc.getPrecision());
            return pdDecDef;
        } else if (pd instanceof PropertyHtmlDefinitionImpl) {
            PropertyHtmlDefinitionImpl pdHtmlDef = new PropertyHtmlDefinitionImpl();
            PropertyHtmlDefinitionImpl pdSrc = (PropertyHtmlDefinitionImpl) pd;
            initializePropertyDefinition(pd, pdHtmlDef);
            pdHtmlDef.setChoices(pdSrc.getChoices());
            pdHtmlDef.setDefaultValue(pdSrc.getDefaultValue());
            return pdHtmlDef;
        } else if (pd instanceof PropertyIdDefinitionImpl) {
            PropertyIdDefinitionImpl pdIdDef = new PropertyIdDefinitionImpl();
            PropertyIdDefinitionImpl pdSrc = (PropertyIdDefinitionImpl) pd;
            initializePropertyDefinition(pd, pdIdDef);
            pdIdDef.setChoices(pdSrc.getChoices());
            pdIdDef.setDefaultValue(pdSrc.getDefaultValue());
            return pdIdDef;
        } else if (pd instanceof PropertyIntegerDefinitionImpl) {
            PropertyIntegerDefinitionImpl pdIntDef = new PropertyIntegerDefinitionImpl();
            PropertyIntegerDefinitionImpl pdSrc = (PropertyIntegerDefinitionImpl) pd;
            initializePropertyDefinition(pd, pdIntDef);
            pdIntDef.setChoices(pdSrc.getChoices());
            pdIntDef.setDefaultValue(pdSrc.getDefaultValue());
            pdIntDef.setMinValue(pdSrc.getMinValue());
            pdIntDef.setMaxValue(pdSrc.getMaxValue());
            return pdIntDef;
        } else if (pd instanceof PropertyStringDefinitionImpl) {
            PropertyStringDefinitionImpl pdStringDef = new PropertyStringDefinitionImpl();
            PropertyStringDefinitionImpl pdSrc = (PropertyStringDefinitionImpl) pd;
            initializePropertyDefinition(pd, pdStringDef);
            pdStringDef.setChoices(pdSrc.getChoices());
            pdStringDef.setDefaultValue(pdSrc.getDefaultValue());
            pdStringDef.setMaxLength(pdSrc.getMaxLength());
            return pdStringDef;
        } else if (pd instanceof PropertyUriDefinitionImpl) {
            PropertyUriDefinitionImpl pdUriDef = new PropertyUriDefinitionImpl();
            PropertyUriDefinitionImpl pdSrc = (PropertyUriDefinitionImpl) pd;
            initializePropertyDefinition(pd, pdUriDef);
            pdUriDef.setChoices(pdSrc.getChoices());
            pdUriDef.setDefaultValue(pdSrc.getDefaultValue());
            return pdUriDef;
        } else {
            return null;
        }
    }

   /* public static ObjectType cloneType(ObjectType type) {
        ObjectType td = new ObjectType();
        td.initialize(type);
        
        return td;
    }*/


    public static String getQueryNameFromId(String id) {
        StringBuilder sb = new StringBuilder(id.length());
        for (int i = 0; i < id.length(); i++) {
            char c = id.charAt(i);
            if (c == '.' || c == ' ' || c == ',' || c == '\'' || c == '"' || c == '\\' || c == '(' || c == ')') {
                sb.append('_');
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    private static void initializePropertyDefinition(PropertyDefinition<?> pdSrc,
            PropertyDefinition<?> pdTarget) {
        pdTarget.setCardinality(pdSrc.getCardinality());
        pdTarget.setDescription(pdSrc.getDescription());
        pdTarget.setDisplayName(pdSrc.getDisplayName());
        pdTarget.setExtensions(pdSrc.getExtensions());
        pdTarget.setId(pdSrc.getId());
        pdTarget.setInherited(false);
        pdTarget.setOpenChoice(pdSrc.isOpenChoice());
        pdTarget.setOrderable(pdSrc.isOrderable());
        pdTarget.setQueryable(pdSrc.isQueryable());
        pdTarget.setRequired(pdSrc.isRequired());
        pdTarget.setLocalName(pdSrc.getLocalName());
        pdTarget.setLocalNamespace(pdSrc.getLocalNamespace());
        pdTarget.setPropertyType(pdSrc.getPropertyType());
        pdTarget.setQueryName(pdSrc.getQueryName());
        pdTarget.setUpdatibility(pdSrc.getUpdatibility());
    }

}
