Search in sources :

Example 1 with CollectionSchema

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

the class RequestBuilderSpecGenerator method generateRootRequestBuilder.

private RootBuilderSpec generateRootRequestBuilder(RootBuilderSpec parentRootBuilder, ResourceSchema resource, String sourceFile, Map<String, String> pathKeyTypes) throws IOException {
    ValidationResult validationResult = ValidateDataAgainstSchema.validate(resource.data(), resource.schema(), new ValidationOptions());
    if (!validationResult.isValid()) {
        throw new IllegalArgumentException(String.format("Resource validation error.  Resource File '%s', Error Details '%s'", sourceFile, validationResult.toString()));
    }
    String packageName = resource.getNamespace();
    String resourceName = CodeUtil.capitalize(resource.getName());
    String className;
    if (_version == RestliVersion.RESTLI_2_0_0) {
        className = getBuilderClassNameByVersion(RestliVersion.RESTLI_2_0_0, null, resource.getName(), true);
    } else {
        className = getBuilderClassNameByVersion(RestliVersion.RESTLI_1_0_0, null, resource.getName(), true);
    }
    RootBuilderSpec rootBuilderSpec = null;
    if (resource.hasCollection()) {
        rootBuilderSpec = new CollectionRootBuilderSpec(resource);
    } else if (resource.hasSimple()) {
        rootBuilderSpec = new SimpleRootBuilderSpec(resource);
    } else if (resource.hasActionsSet()) {
        rootBuilderSpec = new ActionSetRootBuilderSpec(resource);
    } else {
        log.warn("Ignoring unsupported association resource: " + resourceName);
        return null;
    }
    rootBuilderSpec.setNamespace(packageName);
    rootBuilderSpec.setClassName(className);
    if (_version == RestliVersion.RESTLI_2_0_0) {
        rootBuilderSpec.setBaseClassName("BuilderBase");
    }
    rootBuilderSpec.setSourceIdlName(sourceFile);
    String resourcePath = getResourcePath(resource.getPath());
    rootBuilderSpec.setResourcePath(resourcePath);
    List<String> pathKeys = getPathKeys(resourcePath);
    rootBuilderSpec.setPathKeys(pathKeys);
    rootBuilderSpec.setParentRootBuilder(parentRootBuilder);
    StringArray supportsList = null;
    RestMethodSchemaArray restMethods = null;
    FinderSchemaArray finders = null;
    ResourceSchemaArray subresources = null;
    ActionSchemaArray resourceActions = null;
    ActionSchemaArray entityActions = null;
    String keyClass = null;
    if (resource.getCollection() != null) {
        CollectionSchema collection = resource.getCollection();
        String keyName = collection.getIdentifier().getName();
        // Complex key is not supported
        keyClass = collection.getIdentifier().getType();
        pathKeyTypes.put(keyName, collection.getIdentifier().getType());
        supportsList = collection.getSupports();
        restMethods = collection.getMethods();
        finders = collection.getFinders();
        subresources = collection.getEntity().getSubresources();
        resourceActions = collection.getActions();
        entityActions = collection.getEntity().getActions();
    } else if (resource.getSimple() != null) {
        SimpleSchema simpleSchema = resource.getSimple();
        keyClass = "Void";
        supportsList = simpleSchema.getSupports();
        restMethods = simpleSchema.getMethods();
        subresources = simpleSchema.getEntity().getSubresources();
        resourceActions = simpleSchema.getActions();
    } else if (resource.getActionsSet() != null) {
        ActionsSetSchema actionsSet = resource.getActionsSet();
        resourceActions = actionsSet.getActions();
    }
    Set<ResourceMethod> supportedMethods = getSupportedMethods(supportsList);
    if (!supportedMethods.isEmpty()) {
        for (ResourceMethod resourceMethod : supportedMethods) {
            validateResourceMethod(resource, resourceName, resourceMethod);
        }
    }
    List<RootBuilderMethodSpec> restMethodSpecs = new ArrayList<>();
    List<RootBuilderMethodSpec> finderSpecs = new ArrayList<>();
    List<RootBuilderMethodSpec> resourceActionSpecs = new ArrayList<>();
    List<RootBuilderMethodSpec> entityActionSpecs = new ArrayList<>();
    List<RootBuilderSpec> subresourceSpecs = new ArrayList<>();
    String schemaClass = resource.getSchema();
    if (restMethods != null) {
        restMethodSpecs = generateBasicMethods(rootBuilderSpec, keyClass, schemaClass, supportedMethods, restMethods, resourceName, pathKeys, pathKeyTypes);
    }
    if (finders != null) {
        finderSpecs = generateFinders(rootBuilderSpec, finders, keyClass, schemaClass, resourceName, pathKeys, pathKeyTypes);
    }
    if (resourceActions != null) {
        resourceActionSpecs = generateActions(rootBuilderSpec, resourceActions, keyClass, resourceName, pathKeys, pathKeyTypes);
    }
    if (entityActions != null) {
        entityActionSpecs = generateActions(rootBuilderSpec, entityActions, keyClass, resourceName, pathKeys, pathKeyTypes);
    }
    if (subresources != null) {
        subresourceSpecs = generateSubResources(sourceFile, rootBuilderSpec, subresources, pathKeyTypes);
    }
    // assign to rootBuilderClass
    if (rootBuilderSpec instanceof CollectionRootBuilderSpec) {
        CollectionRootBuilderSpec rootBuilder = (CollectionRootBuilderSpec) rootBuilderSpec;
        rootBuilder.setRestMethods(restMethodSpecs);
        rootBuilder.setFinders(finderSpecs);
        rootBuilder.setResourceActions(resourceActionSpecs);
        rootBuilder.setEntityActions(entityActionSpecs);
        rootBuilder.setSubresources(subresourceSpecs);
    } else if (rootBuilderSpec instanceof SimpleRootBuilderSpec) {
        SimpleRootBuilderSpec rootBuilder = (SimpleRootBuilderSpec) rootBuilderSpec;
        rootBuilder.setRestMethods(restMethodSpecs);
        rootBuilder.setResourceActions(resourceActionSpecs);
        rootBuilder.setSubresources(subresourceSpecs);
    } else if (rootBuilderSpec instanceof ActionSetRootBuilderSpec) {
        ActionSetRootBuilderSpec rootBuilder = (ActionSetRootBuilderSpec) rootBuilderSpec;
        rootBuilder.setResourceActions(resourceActionSpecs);
    }
    registerBuilderSpec(rootBuilderSpec);
    return rootBuilderSpec;
}
Also used : RestMethodSchemaArray(com.linkedin.restli.restspec.RestMethodSchemaArray) CollectionSchema(com.linkedin.restli.restspec.CollectionSchema) SimpleSchema(com.linkedin.restli.restspec.SimpleSchema) ArrayList(java.util.ArrayList) ResourceSchemaArray(com.linkedin.restli.restspec.ResourceSchemaArray) ActionSetRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.ActionSetRootBuilderSpec) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) ActionsSetSchema(com.linkedin.restli.restspec.ActionsSetSchema) CollectionRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.CollectionRootBuilderSpec) StringArray(com.linkedin.data.template.StringArray) RootBuilderMethodSpec(com.linkedin.restli.tools.clientgen.builderspec.RootBuilderMethodSpec) FinderSchemaArray(com.linkedin.restli.restspec.FinderSchemaArray) ActionSchemaArray(com.linkedin.restli.restspec.ActionSchemaArray) RootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.RootBuilderSpec) SimpleRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.SimpleRootBuilderSpec) CollectionRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.CollectionRootBuilderSpec) ActionSetRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.ActionSetRootBuilderSpec) SimpleRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.SimpleRootBuilderSpec) ResourceMethod(com.linkedin.restli.common.ResourceMethod)

Example 2 with CollectionSchema

use of com.linkedin.restli.restspec.CollectionSchema 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 CollectionSchema

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

the class TestExamplesGenerator method findCollectionAction.

private static ActionSchema findCollectionAction(ResourceSchema resourceSchema, String actionName) {
    final CollectionSchema collectionSchema = resourceSchema.getCollection();
    if (collectionSchema != null) {
        final ActionSchemaArray actions = collectionSchema.getActions();
        if (actions != null) {
            for (ActionSchema actionSchema : actions) {
                if (actionSchema.getName().equals(actionName)) {
                    return actionSchema;
                }
            }
        }
    }
    final AssociationSchema associationSchema = resourceSchema.getAssociation();
    if (associationSchema != null) {
        final ActionSchemaArray actions = associationSchema.getActions();
        if (actions != null) {
            for (ActionSchema actionSchema : actions) {
                if (actionSchema.getName().equals(actionName)) {
                    return actionSchema;
                }
            }
        }
    }
    return null;
}
Also used : AssociationSchema(com.linkedin.restli.restspec.AssociationSchema) CollectionSchema(com.linkedin.restli.restspec.CollectionSchema) ActionSchemaArray(com.linkedin.restli.restspec.ActionSchemaArray) ActionSchema(com.linkedin.restli.restspec.ActionSchema)

Example 4 with CollectionSchema

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

the class TestExamplesGenerator method findEntityAction.

private static ActionSchema findEntityAction(ResourceSchema resourceSchema, String actionName) {
    ActionSchema actionSchema;
    final CollectionSchema collectionSchema = resourceSchema.getCollection();
    if (collectionSchema != null) {
        final EntitySchema entity = collectionSchema.getEntity();
        if (entity != null) {
            actionSchema = findActionInEntity(entity, actionName);
            if (actionSchema != null) {
                return actionSchema;
            }
        }
    }
    final AssociationSchema associationSchema = resourceSchema.getAssociation();
    if (associationSchema != null) {
        final EntitySchema entity = associationSchema.getEntity();
        if (entity != null) {
            actionSchema = findActionInEntity(entity, actionName);
            if (actionSchema != null) {
                return actionSchema;
            }
        }
    }
    return null;
}
Also used : AssociationSchema(com.linkedin.restli.restspec.AssociationSchema) CollectionSchema(com.linkedin.restli.restspec.CollectionSchema) EntitySchema(com.linkedin.restli.restspec.EntitySchema) ActionSchema(com.linkedin.restli.restspec.ActionSchema)

Example 5 with CollectionSchema

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

the class TestExamplesGenerator method findFinder.

private static FinderSchema findFinder(ResourceSchema resourceSchema, String finderName) {
    final CollectionSchema collectionSchema = resourceSchema.getCollection();
    if (collectionSchema != null) {
        final FinderSchemaArray finders = collectionSchema.getFinders();
        if (finders != null) {
            for (FinderSchema finderSchema : finders) {
                if (finderSchema.getName().equals(finderName)) {
                    return finderSchema;
                }
            }
        }
    }
    final AssociationSchema associationSchema = resourceSchema.getAssociation();
    if (associationSchema != null) {
        final FinderSchemaArray finders = associationSchema.getFinders();
        if (finders != null) {
            for (FinderSchema finderSchema : finders) {
                if (finderSchema.getName().equals(finderName)) {
                    return finderSchema;
                }
            }
        }
    }
    return null;
}
Also used : AssociationSchema(com.linkedin.restli.restspec.AssociationSchema) CollectionSchema(com.linkedin.restli.restspec.CollectionSchema) FinderSchemaArray(com.linkedin.restli.restspec.FinderSchemaArray) FinderSchema(com.linkedin.restli.restspec.FinderSchema)

Aggregations

CollectionSchema (com.linkedin.restli.restspec.CollectionSchema)11 AssociationSchema (com.linkedin.restli.restspec.AssociationSchema)9 SimpleSchema (com.linkedin.restli.restspec.SimpleSchema)6 ActionsSetSchema (com.linkedin.restli.restspec.ActionsSetSchema)5 StringArray (com.linkedin.data.template.StringArray)4 ActionSchemaArray (com.linkedin.restli.restspec.ActionSchemaArray)4 FinderSchemaArray (com.linkedin.restli.restspec.FinderSchemaArray)4 RestMethodSchemaArray (com.linkedin.restli.restspec.RestMethodSchemaArray)4 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)3 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)3 ResourceMethod (com.linkedin.restli.common.ResourceMethod)3 ActionSchema (com.linkedin.restli.restspec.ActionSchema)3 ResourceSchemaArray (com.linkedin.restli.restspec.ResourceSchemaArray)3 RestMethodSchema (com.linkedin.restli.restspec.RestMethodSchema)3 ArrayList (java.util.ArrayList)3 DataList (com.linkedin.data.DataList)2 DataMap (com.linkedin.data.DataMap)2 RestliRequestOptions (com.linkedin.restli.client.RestliRequestOptions)2 URIParamUtils (com.linkedin.restli.internal.common.URIParamUtils)2 EntitySchema (com.linkedin.restli.restspec.EntitySchema)2