Search in sources :

Example 1 with BatchFinderSchema

use of com.linkedin.restli.restspec.BatchFinderSchema in project rest.li by linkedin.

the class JavaRequestBuilderGenerator method generateQueryParamBindingMethods.

private void generateQueryParamBindingMethods(JDefinedClass facadeClass, ParameterSchemaArray parameters, JDefinedClass derivedBuilderClass, RecordTemplate methodSchema) {
    for (ParameterSchema param : parameters) {
        if ("array".equals(param.getType())) {
            final JClass paramItemsClass = getJavaBindingType(param.getItems(), facadeClass).valueClass;
            final JClass paramClass = getCodeModel().ref(Iterable.class).narrow(paramItemsClass);
            generateQueryParamSetMethod(derivedBuilderClass, param, paramClass, paramItemsClass);
            generateQueryParamAddMethod(derivedBuilderClass, param, paramItemsClass);
        } else {
            final DataSchema typeSchema = RestSpecCodec.textToSchema(param.getType(), _schemaResolver);
            final JClass paramClass = getJavaBindingType(typeSchema, facadeClass).valueClass;
            // since we can not guarantee that SearchCriteraArray is generated
            if (!(methodSchema instanceof BatchFinderSchema && ((BatchFinderSchema) methodSchema).getBatchParam().equals(param.getName()))) {
                generateQueryParamSetMethod(derivedBuilderClass, param, paramClass, paramClass);
            }
            // for backwards compatibility, add the method with Iterable<Foo> parameter
            if (typeSchema instanceof ArrayDataSchema) {
                final DataSchema itemsSchema = ((ArrayDataSchema) typeSchema).getItems();
                final JClass paramItemsClass = getJavaBindingType(itemsSchema, facadeClass).valueClass;
                final JClass iterableItemsClass = getCodeModel().ref(Iterable.class).narrow(paramItemsClass);
                generateQueryParamSetMethod(derivedBuilderClass, param, iterableItemsClass, paramItemsClass);
                generateQueryParamAddMethod(derivedBuilderClass, param, paramItemsClass);
            }
        }
    }
}
Also used : TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) JClass(com.sun.codemodel.JClass) ParameterSchema(com.linkedin.restli.restspec.ParameterSchema) BatchFinderSchema(com.linkedin.restli.restspec.BatchFinderSchema)

Example 2 with BatchFinderSchema

use of com.linkedin.restli.restspec.BatchFinderSchema in project rest.li by linkedin.

the class SnapshotGenerator method findModelsCollection.

private void findModelsCollection(ResourceSchema resourceSchema, Map<String, NamedDataSchema> foundTypes, List<NamedDataSchema> typeOrder) {
    CollectionSchema collection = resourceSchema.getCollection();
    if (collection != null) {
        IdentifierSchema identifier = collection.getIdentifier();
        findModelsIdentifier(identifier, foundTypes, typeOrder);
        findErrorDetailTypeModels(collection, foundTypes, typeOrder);
        if (collection.hasFinders()) {
            for (FinderSchema restMethodSchema : collection.getFinders()) {
                findModels(restMethodSchema.getParameters(), restMethodSchema.getMetadata(), foundTypes, typeOrder);
                findErrorDetailTypeModels(restMethodSchema, foundTypes, typeOrder);
            }
        }
        if (collection.hasBatchFinders()) {
            for (BatchFinderSchema restMethodSchema : collection.getBatchFinders()) {
                findModels(restMethodSchema.getParameters(), restMethodSchema.getMetadata(), foundTypes, typeOrder);
                findErrorDetailTypeModels(restMethodSchema, foundTypes, typeOrder);
            }
        }
        if (collection.hasMethods()) {
            for (RestMethodSchema restMethodSchema : collection.getMethods()) {
                findModels(restMethodSchema.getParameters(), restMethodSchema.getMetadata(), foundTypes, typeOrder);
                findErrorDetailTypeModels(restMethodSchema, foundTypes, typeOrder);
            }
        }
        if (collection.hasActions()) {
            for (ActionSchema actionSchema : collection.getActions()) {
                findModelsAction(actionSchema, foundTypes, typeOrder);
                findErrorDetailTypeModels(actionSchema, foundTypes, typeOrder);
            }
        }
        if (collection.hasEntity()) {
            EntitySchema entity = collection.getEntity();
            findModelsEntity(entity, foundTypes, typeOrder);
        }
    }
}
Also used : CollectionSchema(com.linkedin.restli.restspec.CollectionSchema) IdentifierSchema(com.linkedin.restli.restspec.IdentifierSchema) BatchFinderSchema(com.linkedin.restli.restspec.BatchFinderSchema) RestMethodSchema(com.linkedin.restli.restspec.RestMethodSchema) BatchFinderSchema(com.linkedin.restli.restspec.BatchFinderSchema) FinderSchema(com.linkedin.restli.restspec.FinderSchema) EntitySchema(com.linkedin.restli.restspec.EntitySchema) ActionSchema(com.linkedin.restli.restspec.ActionSchema)

Example 3 with BatchFinderSchema

use of com.linkedin.restli.restspec.BatchFinderSchema in project rest.li by linkedin.

the class SnapshotGenerator method findModelsAssocation.

private void findModelsAssocation(ResourceSchema resourceSchema, Map<String, NamedDataSchema> foundTypes, List<NamedDataSchema> typeOrder) {
    AssociationSchema association = resourceSchema.getAssociation();
    if (association != null) {
        findErrorDetailTypeModels(association, foundTypes, typeOrder);
        for (AssocKeySchema assocKeySchema : association.getAssocKeys()) {
            String type = assocKeySchema.getType();
            recordType(type, foundTypes, typeOrder);
        }
        if (association.hasFinders()) {
            for (FinderSchema restMethodSchema : association.getFinders()) {
                findModels(restMethodSchema.getParameters(), restMethodSchema.getMetadata(), foundTypes, typeOrder);
                findErrorDetailTypeModels(restMethodSchema, foundTypes, typeOrder);
            }
        }
        if (association.hasBatchFinders()) {
            for (BatchFinderSchema restMethodSchema : association.getBatchFinders()) {
                findModels(restMethodSchema.getParameters(), restMethodSchema.getMetadata(), foundTypes, typeOrder);
                findErrorDetailTypeModels(restMethodSchema, foundTypes, typeOrder);
            }
        }
        if (association.hasMethods()) {
            for (RestMethodSchema restMethodSchema : association.getMethods()) {
                findModels(restMethodSchema.getParameters(), restMethodSchema.getMetadata(), foundTypes, typeOrder);
                findErrorDetailTypeModels(restMethodSchema, foundTypes, typeOrder);
            }
        }
        if (association.hasActions()) {
            for (ActionSchema actionSchema : association.getActions()) {
                findModelsAction(actionSchema, foundTypes, typeOrder);
                findErrorDetailTypeModels(actionSchema, foundTypes, typeOrder);
            }
        }
        if (association.hasEntity()) {
            EntitySchema entitySchema = association.getEntity();
            findModelsEntity(entitySchema, foundTypes, typeOrder);
        }
    }
}
Also used : AssociationSchema(com.linkedin.restli.restspec.AssociationSchema) BatchFinderSchema(com.linkedin.restli.restspec.BatchFinderSchema) RestMethodSchema(com.linkedin.restli.restspec.RestMethodSchema) BatchFinderSchema(com.linkedin.restli.restspec.BatchFinderSchema) FinderSchema(com.linkedin.restli.restspec.FinderSchema) EntitySchema(com.linkedin.restli.restspec.EntitySchema) ActionSchema(com.linkedin.restli.restspec.ActionSchema) AssocKeySchema(com.linkedin.restli.restspec.AssocKeySchema)

Example 4 with BatchFinderSchema

use of com.linkedin.restli.restspec.BatchFinderSchema in project rest.li by linkedin.

the class JavaRequestBuilderGenerator method generateBatchFinders.

private void generateBatchFinders(JDefinedClass facadeClass, JExpression baseUriExpr, BatchFinderSchemaArray batchFinderSchemas, JClass keyClass, JClass valueClass, Map<String, AssocKeyTypeInfo> assocKeys, JVar resourceSpecField, String resourceName, List<String> pathKeys, Map<String, JClass> pathKeyTypes, Map<String, JClass> assocKeyTypes, Map<String, List<String>> pathToAssocKeys, JExpression requestOptionsExpr, String rootPath) throws JClassAlreadyExistsException {
    if (batchFinderSchemas != null) {
        final JClass baseBuilderClass = getCodeModel().ref(BatchFindRequestBuilderBase.class).narrow(keyClass, valueClass);
        for (BatchFinderSchema batchFinder : batchFinderSchemas) {
            final String batchFinderName = batchFinder.getName();
            final String builderName = CodeUtil.capitalize(resourceName) + "BatchFindBy" + CodeUtil.capitalize(batchFinderName) + METHOD_BUILDER_SUFFIX.get(_version);
            JDefinedClass batchFinderBuilderClass = generateDerivedBuilder(baseBuilderClass, valueClass, batchFinderName, builderName, facadeClass.getPackage(), ResourceMethod.BATCH_FINDER, null, rootPath);
            final JMethod batchFinderMethod = facadeClass.method(JMod.PUBLIC, batchFinderBuilderClass, "batchFindBy" + CodeUtil.capitalize(batchFinderName));
            batchFinderMethod.body()._return(JExpr._new(batchFinderBuilderClass).arg(baseUriExpr).arg(resourceSpecField).arg(requestOptionsExpr));
            final Set<String> batchFinderKeys = new TreeSet<>();
            if (batchFinder.getAssocKey() != null) {
                batchFinderKeys.add(batchFinder.getAssocKey());
            }
            if (batchFinder.getAssocKeys() != null) {
                for (String assocKey : batchFinder.getAssocKeys()) {
                    batchFinderKeys.add(assocKey);
                }
            }
            generatePathKeyBindingMethods(pathKeys, batchFinderBuilderClass, pathKeyTypes, assocKeyTypes, pathToAssocKeys);
            generateAssocKeyBindingMethods(assocKeys, batchFinderBuilderClass, batchFinderKeys);
            if (batchFinder.getParameters() != null) {
                generateQueryParamBindingMethods(facadeClass, batchFinder.getParameters(), batchFinderBuilderClass, batchFinder);
            }
            // process the metadata schema file
            if (batchFinder.getMetadata() != null) {
                final String metadataClass = batchFinder.getMetadata().getType();
                getJavaBindingType(metadataClass, facadeClass);
            }
            generateClassJavadoc(batchFinderBuilderClass, batchFinder);
            generateFactoryMethodJavadoc(batchFinderMethod, batchFinder);
        }
    }
}
Also used : BatchFindRequestBuilderBase(com.linkedin.restli.client.base.BatchFindRequestBuilderBase) JDefinedClass(com.sun.codemodel.JDefinedClass) TreeSet(java.util.TreeSet) JClass(com.sun.codemodel.JClass) BatchFinderSchema(com.linkedin.restli.restspec.BatchFinderSchema) JMethod(com.sun.codemodel.JMethod)

Example 5 with BatchFinderSchema

use of com.linkedin.restli.restspec.BatchFinderSchema in project rest.li by linkedin.

the class RestLiHTMLDocumentationRenderer method renderResource.

@Override
public void renderResource(String resourceName, OutputStream out) {
    final ResourceSchema resourceSchema = _resourceSchemas.getResource(resourceName);
    final List<ResourceSchema> parentResources = _resourceSchemas.getParentResources(resourceSchema);
    ExampleRequestResponseGenerator generator = new ExampleRequestResponseGenerator(parentResources, resourceSchema, _schemaResolver);
    if (resourceSchema == null) {
        throw new RoutingException(String.format("Resource \"%s\" does not exist", resourceName), HttpStatus.S_404_NOT_FOUND.getCode());
    }
    final Map<String, Object> pageModel = createPageModel();
    pageModel.put("resource", resourceSchema);
    pageModel.put("resourceName", resourceName);
    pageModel.put("resourceFullName", ResourceSchemaUtil.getFullName(resourceSchema));
    pageModel.put("resourceType", getResourceType(resourceSchema));
    pageModel.put("subResources", _resourceSchemas.getSubResources(resourceSchema));
    final List<ResourceMethodDocView> restMethods = new ArrayList<>();
    final List<ResourceMethodDocView> finders = new ArrayList<>();
    final List<ResourceMethodDocView> batchFinders = new ArrayList<>();
    final List<ResourceMethodDocView> actions = new ArrayList<>();
    final MethodGatheringResourceSchemaVisitor visitor = new MethodGatheringResourceSchemaVisitor(resourceName);
    ResourceSchemaCollection.visitResources(_resourceSchemas.getResources().values(), visitor);
    for (RecordTemplate methodSchema : visitor.getAllMethods()) {
        final ExampleRequestResponse capture;
        if (methodSchema instanceof RestMethodSchema) {
            RestMethodSchema restMethodSchema = (RestMethodSchema) methodSchema;
            capture = generator.method(ResourceMethod.valueOf(restMethodSchema.getMethod().toUpperCase()));
        } else if (methodSchema instanceof FinderSchema) {
            FinderSchema finderMethodSchema = (FinderSchema) methodSchema;
            capture = generator.finder(finderMethodSchema.getName());
        } else if (methodSchema instanceof BatchFinderSchema) {
            BatchFinderSchema batchFinderSchema = (BatchFinderSchema) methodSchema;
            capture = generator.batchFinder(batchFinderSchema.getName());
        } else if (methodSchema instanceof ActionSchema) {
            ActionSchema actionMethodSchema = (ActionSchema) methodSchema;
            final ResourceLevel resourceLevel = (visitor.getCollectionActions().contains(methodSchema) ? ResourceLevel.COLLECTION : ResourceLevel.ENTITY);
            capture = generator.action(actionMethodSchema.getName(), resourceLevel);
        } else {
            capture = null;
        }
        String requestEntity = null;
        String responseEntity = null;
        if (capture != null) {
            try {
                DataMap entityMap;
                if (capture.getRequest().getEntity().length() > 0) {
                    entityMap = DataMapUtils.readMap(capture.getRequest());
                    requestEntity = new String(_codec.mapToBytes(entityMap));
                }
                if (capture.getResponse() != null && capture.getResponse().getEntity() != null && capture.getResponse().getEntity().length() > 0) {
                    entityMap = DataMapUtils.readMap(capture.getResponse());
                    responseEntity = new String(_codec.mapToBytes(entityMap));
                }
            } catch (IOException e) {
                throw new RestLiInternalException(e);
            }
        }
        final ResourceMethodDocView docView = new ResourceMethodDocView(methodSchema, capture, getDoc(methodSchema, resourceSchema.hasSimple()), requestEntity, responseEntity);
        if (methodSchema instanceof RestMethodSchema) {
            restMethods.add(docView);
        } else if (methodSchema instanceof FinderSchema) {
            finders.add(docView);
        } else if (methodSchema instanceof BatchFinderSchema) {
            batchFinders.add(docView);
        } else if (methodSchema instanceof ActionSchema) {
            actions.add(docView);
        }
    }
    pageModel.put("restMethods", restMethods);
    pageModel.put("finders", finders);
    pageModel.put("batchFinders", batchFinders);
    pageModel.put("actions", actions);
    addRelated(resourceSchema, pageModel);
    final ServiceErrorGatheringVisitor serviceErrorGatheringVisitor = new ServiceErrorGatheringVisitor();
    ResourceSchemaCollection.visitResources(Collections.singletonList(resourceSchema), serviceErrorGatheringVisitor);
    pageModel.put("serviceErrors", serviceErrorGatheringVisitor.getServiceErrors());
    pageModel.put("resourceLevelServiceErrors", serviceErrorGatheringVisitor.getResourceLevelServiceErrors());
    // The following two flags are necessary for conditionally displaying the columns of the REST methods table
    pageModel.put("restMethodsHaveSuccessStatuses", serviceErrorGatheringVisitor.doRestMethodsHaveSuccessStatuses());
    pageModel.put("restMethodsHaveServiceErrors", serviceErrorGatheringVisitor.doRestMethodsHaveServiceErrors());
    pageModel.put("util", new DocumentationTemplateUtil());
    _templatingEngine.render("resource/index.vm", pageModel, out);
}
Also used : RoutingException(com.linkedin.restli.server.RoutingException) ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) ExampleRequestResponse(com.linkedin.restli.docgen.examplegen.ExampleRequestResponse) ResourceLevel(com.linkedin.restli.server.ResourceLevel) ArrayList(java.util.ArrayList) BatchFinderSchema(com.linkedin.restli.restspec.BatchFinderSchema) RestMethodSchema(com.linkedin.restli.restspec.RestMethodSchema) BatchFinderSchema(com.linkedin.restli.restspec.BatchFinderSchema) FinderSchema(com.linkedin.restli.restspec.FinderSchema) IOException(java.io.IOException) ActionSchema(com.linkedin.restli.restspec.ActionSchema) DataMap(com.linkedin.data.DataMap) ExampleRequestResponseGenerator(com.linkedin.restli.docgen.examplegen.ExampleRequestResponseGenerator) RecordTemplate(com.linkedin.data.template.RecordTemplate) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException)

Aggregations

BatchFinderSchema (com.linkedin.restli.restspec.BatchFinderSchema)9 ActionSchema (com.linkedin.restli.restspec.ActionSchema)5 FinderSchema (com.linkedin.restli.restspec.FinderSchema)5 RestMethodSchema (com.linkedin.restli.restspec.RestMethodSchema)4 DataMap (com.linkedin.data.DataMap)2 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)2 DataSchema (com.linkedin.data.schema.DataSchema)2 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)2 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)2 RecordTemplate (com.linkedin.data.template.RecordTemplate)2 StringArray (com.linkedin.data.template.StringArray)2 AssocKeySchema (com.linkedin.restli.restspec.AssocKeySchema)2 AssociationSchema (com.linkedin.restli.restspec.AssociationSchema)2 CollectionSchema (com.linkedin.restli.restspec.CollectionSchema)2 EntitySchema (com.linkedin.restli.restspec.EntitySchema)2 IdentifierSchema (com.linkedin.restli.restspec.IdentifierSchema)2 ParameterSchema (com.linkedin.restli.restspec.ParameterSchema)2 ResourceSchema (com.linkedin.restli.restspec.ResourceSchema)2 ResourceLevel (com.linkedin.restli.server.ResourceLevel)2 DataList (com.linkedin.data.DataList)1