Package com.couchace.core.api.meta

Source Code of com.couchace.core.api.meta.AnnotationMetaBuilder

/*
* Copyright 2012 Harlan Noonkester
*
* 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 com.couchace.core.api.meta;

import com.couchace.core.api.CouchException;
import com.couchace.core.api.annotation.CouchAttachment;
import com.couchace.core.api.annotation.CouchEntity;
import com.couchace.core.api.annotation.CouchId;
import com.couchace.core.api.annotation.CouchRevision;
import com.couchace.core.internal.util.ClassUtil;
import com.couchace.core.internal.util.StringUtil;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
* User: Harlan
* Date: 3/14/2014
* Time: 9:25 AM
*/
public class AnnotationMetaBuilder implements MetaBuilder {

    public <T> EntityMeta<T> buildEntityMeta(Class<T> entityClass) {
        List<AttachmentMeta> localAttachmentList = new ArrayList<>();

        // Look for class level annotations.
        CouchEntity couchEntity = ClassUtil.findClassAnnotation(entityClass, CouchEntity.class);
        if (couchEntity == null) {
            throw CouchException.internalServerError("Class " + entityClass.getName() + " does not have required @CouchEntity annotation.");
        }
        String entityType;
        if (StringUtil.isNotBlank(couchEntity.value())) {
            entityType = couchEntity.value();
        } else {
            entityType = entityClass.getSimpleName();
        }

        // Look for method level annotations.
        ValueAccessor localIdAccessor = null;
        ValueAccessor localRevisionAccessor = null;
        String localIdPattern = null;
        for (Method readMethod : ClassUtil.listGetterMethods(entityClass)) {
            if (readMethod != null) {
                CouchAttachment couchAttachment = readMethod.getAnnotation(CouchAttachment.class);
                if (couchAttachment != null) {
                    ValueAccessor attachmentAccessor = new ValueAccessor(entityClass, readMethod);
                    localAttachmentList.add(new AttachmentMeta(couchAttachment, attachmentAccessor));
                }
                if (localRevisionAccessor == null) {
                    CouchRevision couchRevision = readMethod.getAnnotation(CouchRevision.class);
                    if (couchRevision != null) {
                        localRevisionAccessor = new ValueAccessor(entityClass, readMethod);
                    }
                }
                if (localIdAccessor == null) {
                    CouchId couchId = readMethod.getAnnotation(CouchId.class);
                    if (couchId != null) {
                        localIdAccessor = new ValueAccessor(entityClass, readMethod);
                        localIdPattern = (StringUtil.isNotBlank(couchId.pattern())) ? couchId.pattern() : null;
                    }
                }
            }
        }

        // Id accessor / annotation is mandatory
        if (localIdAccessor == null) {
            throw CouchException.internalServerError("Class " + entityClass.getName() + " does not have required @CouchId annotation.");
        }

        return new EntityMeta<>(entityClass.getName(),
                entityClass,
                entityType,
                localIdAccessor,
                localIdPattern,
                localRevisionAccessor,
                localAttachmentList);
    }
}
TOP

Related Classes of com.couchace.core.api.meta.AnnotationMetaBuilder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.