Search in sources :

Example 1 with BatchGetEntityRequestBuilderBase

use of com.linkedin.restli.client.base.BatchGetEntityRequestBuilderBase in project rest.li by linkedin.

the class JavaRequestBuilderGenerator method generateDerivedBuilder.

private JDefinedClass generateDerivedBuilder(JClass baseBuilderClass, JClass valueClass, String finderName, String derivedBuilderName, JPackage clientPackage, ResourceMethod resourceMethod, DataMap annotations, String rootPath) throws JClassAlreadyExistsException {
    // this method applies to REST methods and finder
    final JDefinedClass derivedBuilderClass = clientPackage._class(JMod.PUBLIC, derivedBuilderName);
    annotate(derivedBuilderClass, null, rootPath);
    checkVersionAndDeprecateBuilderClass(derivedBuilderClass, false);
    derivedBuilderClass._extends(baseBuilderClass.narrow(derivedBuilderClass));
    final JMethod derivedBuilderConstructor = derivedBuilderClass.constructor(JMod.PUBLIC);
    final JVar uriParam = derivedBuilderConstructor.param(_stringClass, "baseUriTemplate");
    final JVar resourceSpecParam = derivedBuilderConstructor.param(_resourceSpecClass, "resourceSpec");
    final JVar requestOptionsParam = derivedBuilderConstructor.param(RestliRequestOptions.class, "requestOptions");
    final JInvocation invocation = derivedBuilderConstructor.body().invoke(SUPER).arg(uriParam);
    // the new BatchGetEntityRequestBuilderBase does not need the valueClass parameter in constructor
    if (!baseBuilderClass.fullName().startsWith(BatchGetEntityRequestBuilderBase.class.getName() + "<")) {
        invocation.arg(valueClass.dotclass());
    }
    invocation.arg(resourceSpecParam).arg(requestOptionsParam);
    if (finderName != null) {
        derivedBuilderConstructor.body().add(JExpr._super().invoke("name").arg(finderName));
    }
    if (_validateEntityMethods.contains(resourceMethod) || _validatePatchMethods.contains(resourceMethod)) {
        JMethod validateMethod = derivedBuilderClass.method(JMod.PUBLIC | JMod.STATIC, ValidationResult.class, "validateInput");
        JVar inputParam;
        if (_validateEntityMethods.contains(resourceMethod)) {
            inputParam = validateMethod.param(valueClass, "input");
        } else {
            inputParam = validateMethod.param(getCodeModel().ref(PatchRequest.class).narrow(valueClass), "patch");
        }
        JBlock block = validateMethod.body();
        JVar annotationMap = block.decl(getCodeModel().ref(Map.class).narrow(String.class).narrow(getCodeModel().ref(List.class).narrow(String.class)), "annotations").init(JExpr._new(getCodeModel().ref(HashMap.class).narrow(String.class).narrow(getCodeModel().ref(List.class).narrow(String.class))));
        if (annotations != null) {
            for (Map.Entry<String, Object> entry : annotations.entrySet()) {
                DataList values = ((DataMap) entry.getValue()).getDataList("value");
                if (values != null) {
                    JInvocation list = getCodeModel().ref(Arrays.class).staticInvoke("asList");
                    for (Object value : values) {
                        list.arg(value.toString());
                    }
                    block.add(annotationMap.invoke("put").arg(entry.getKey()).arg(list));
                }
            }
        }
        JClass validatorClass = getCodeModel().ref(RestLiDataValidator.class);
        JVar validator = block.decl(validatorClass, "validator").init(JExpr._new(validatorClass).arg(annotationMap).arg(valueClass.dotclass()).arg(getCodeModel().ref(ResourceMethod.class).staticRef(resourceMethod.name())));
        block._return(validator.invoke("validateInput").arg(inputParam));
    }
    return derivedBuilderClass;
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) JClass(com.sun.codemodel.JClass) JInvocation(com.sun.codemodel.JInvocation) PatchRequest(com.linkedin.restli.common.PatchRequest) DataMap(com.linkedin.data.DataMap) DataList(com.linkedin.data.DataList) JBlock(com.sun.codemodel.JBlock) BatchGetEntityRequestBuilderBase(com.linkedin.restli.client.base.BatchGetEntityRequestBuilderBase) JMethod(com.sun.codemodel.JMethod) Arrays(java.util.Arrays) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) DataMap(com.linkedin.data.DataMap) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) ResourceMethod(com.linkedin.restli.common.ResourceMethod) JVar(com.sun.codemodel.JVar)

Aggregations

DataList (com.linkedin.data.DataList)1 DataMap (com.linkedin.data.DataMap)1 BatchGetEntityRequestBuilderBase (com.linkedin.restli.client.base.BatchGetEntityRequestBuilderBase)1 PatchRequest (com.linkedin.restli.common.PatchRequest)1 ResourceMethod (com.linkedin.restli.common.ResourceMethod)1 JBlock (com.sun.codemodel.JBlock)1 JClass (com.sun.codemodel.JClass)1 JDefinedClass (com.sun.codemodel.JDefinedClass)1 JInvocation (com.sun.codemodel.JInvocation)1 JMethod (com.sun.codemodel.JMethod)1 JVar (com.sun.codemodel.JVar)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1