Search in sources :

Example 1 with ActionSchemaArray

use of com.linkedin.restli.restspec.ActionSchemaArray 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 2 with ActionSchemaArray

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

the class JavaRequestBuilderGenerator method responseMetadataMapInit.

private // lets work on this first...
JVar responseMetadataMapInit(// lets work on this first...
JDefinedClass facadeClass, // lets work on this first...
ActionSchemaArray resourceActions, // lets work on this first...
ActionSchemaArray entityActions, // lets work on this first...
JBlock staticInit) {
    final JClass MetadataMapClass = getCodeModel().ref(HashMap.class).narrow(_stringClass, getCodeModel().ref(DynamicRecordMetadata.class));
    final JVar responseMetadataMap = staticInit.decl(MetadataMapClass, "responseMetadataMap").init(JExpr._new(MetadataMapClass));
    // get all actions into a single ActionSchemaArray
    final int resourceActionsSize = resourceActions == null ? 0 : resourceActions.size();
    final int entityActionsSize = entityActions == null ? 0 : entityActions.size();
    final ActionSchemaArray allActionSchema = new ActionSchemaArray(resourceActionsSize + entityActionsSize);
    allActionSchema.addAll(resourceActions == null ? new ActionSchemaArray() : resourceActions);
    allActionSchema.addAll(entityActions == null ? new ActionSchemaArray() : entityActions);
    final String returnName = "value";
    for (ActionSchema actionSchema : allActionSchema) {
        final String methodName = actionSchema.getName();
        final JInvocation returnFieldDefs;
        if (actionSchema.hasReturns()) {
            final JInvocation returnFieldDef = createFieldDef(returnName, actionSchema.getReturns(), facadeClass);
            returnFieldDefs = getCodeModel().ref(Collections.class).staticInvoke("singletonList").arg(returnFieldDef);
        } else {
            returnFieldDefs = getCodeModel().ref(Collections.class).staticInvoke("<FieldDef<?>>emptyList");
        }
        final JInvocation returnMetadata = createMetadata(methodName, returnFieldDefs);
        staticInit.add(responseMetadataMap.invoke("put").arg(methodName).arg(returnMetadata));
    }
    return responseMetadataMap;
}
Also used : DynamicRecordMetadata(com.linkedin.data.template.DynamicRecordMetadata) HashMap(java.util.HashMap) JClass(com.sun.codemodel.JClass) ActionSchemaArray(com.linkedin.restli.restspec.ActionSchemaArray) JInvocation(com.sun.codemodel.JInvocation) Collections(java.util.Collections) ActionSchema(com.linkedin.restli.restspec.ActionSchema) JVar(com.sun.codemodel.JVar)

Example 3 with ActionSchemaArray

use of com.linkedin.restli.restspec.ActionSchemaArray 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(RequiredMode.MUST_BE_PRESENT));
    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 {
        throw new IllegalArgumentException("unsupported resource type for resource: '" + resourceName + '\'');
    }
    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<RootBuilderMethodSpec>();
    List<RootBuilderMethodSpec> finderSpecs = new ArrayList<RootBuilderMethodSpec>();
    List<RootBuilderMethodSpec> resourceActionSpecs = new ArrayList<RootBuilderMethodSpec>();
    List<RootBuilderMethodSpec> entityActionSpecs = new ArrayList<RootBuilderMethodSpec>();
    List<RootBuilderSpec> subresourceSpecs = new ArrayList<RootBuilderSpec>();
    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 4 with ActionSchemaArray

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

the class ResourceSchemaToResourceSpecTranslator method toDynamicRecordMetadata.

private ActionCollectionMetadata toDynamicRecordMetadata(ActionSchemaArray actions, ActionSchemaArray entityActions) {
    Map<String, DynamicRecordMetadata> response = new HashMap<String, DynamicRecordMetadata>();
    Map<String, DynamicRecordMetadata> request = new HashMap<String, DynamicRecordMetadata>();
    ActionSchemaArray[] actionGroups = new ActionSchemaArray[] { actions, entityActions };
    for (ActionSchemaArray actionGroup : actionGroups) {
        if (actionGroup != null) {
            for (ActionSchema action : actionGroup) {
                ActionMetadata metadata = toActionMetadata(action);
                request.put(metadata._name, metadata._request);
                response.put(metadata._name, metadata._response);
            }
        }
    }
    return new ActionCollectionMetadata(request, response);
}
Also used : DynamicRecordMetadata(com.linkedin.data.template.DynamicRecordMetadata) HashMap(java.util.HashMap) ActionSchemaArray(com.linkedin.restli.restspec.ActionSchemaArray) ActionSchema(com.linkedin.restli.restspec.ActionSchema)

Example 5 with ActionSchemaArray

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

the class ResourceSchemaToResourceSpecTranslator method associationToResourceSpec.

private ResourceSpec associationToResourceSpec(ResourceSchema resourceSchema, AssociationSchema association) {
    ActionSchemaArray actions = null, entityActions = null;
    StringArray supports = association.getSupports();
    if (association.hasActions()) {
        actions = association.getActions();
    }
    if (association.getEntity().hasActions()) {
        entityActions = association.getEntity().getActions();
    }
    String schema = resourceSchema.getSchema();
    AssocKeySchemaArray assocKeys = association.getAssocKeys();
    Map<String, CompoundKey.TypeInfo> keyParts = new HashMap<String, CompoundKey.TypeInfo>();
    for (AssocKeySchema assocKey : assocKeys) {
        TypeSpec<?> type = toTypeSpec(RestSpecCodec.textToSchema(assocKey.getType(), _schemaResolver));
        keyParts.put(assocKey.getName(), new CompoundKey.TypeInfo(type, type));
    }
    return buildResourceSpec(supports, new TypeSpec<CompoundKey>(CompoundKey.class, null), null, keyParts, schema, actions, entityActions);
}
Also used : AssocKeySchemaArray(com.linkedin.restli.restspec.AssocKeySchemaArray) StringArray(com.linkedin.data.template.StringArray) HashMap(java.util.HashMap) CompoundKey(com.linkedin.restli.common.CompoundKey) ActionSchemaArray(com.linkedin.restli.restspec.ActionSchemaArray) AssocKeySchema(com.linkedin.restli.restspec.AssocKeySchema)

Aggregations

ActionSchemaArray (com.linkedin.restli.restspec.ActionSchemaArray)15 StringArray (com.linkedin.data.template.StringArray)5 ActionSchema (com.linkedin.restli.restspec.ActionSchema)5 HashMap (java.util.HashMap)5 CollectionSchema (com.linkedin.restli.restspec.CollectionSchema)4 FinderSchemaArray (com.linkedin.restli.restspec.FinderSchemaArray)4 DataMap (com.linkedin.data.DataMap)3 DynamicRecordMetadata (com.linkedin.data.template.DynamicRecordMetadata)3 ActionsSetSchema (com.linkedin.restli.restspec.ActionsSetSchema)3 AssociationSchema (com.linkedin.restli.restspec.AssociationSchema)3 ResourceSchemaArray (com.linkedin.restli.restspec.ResourceSchemaArray)3 SimpleSchema (com.linkedin.restli.restspec.SimpleSchema)3 JClass (com.sun.codemodel.JClass)3 JInvocation (com.sun.codemodel.JInvocation)3 JVar (com.sun.codemodel.JVar)3 ArrayList (java.util.ArrayList)3 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)2 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)2 ResourceMethod (com.linkedin.restli.common.ResourceMethod)2 CustomAnnotationContentSchemaMap (com.linkedin.restli.restspec.CustomAnnotationContentSchemaMap)2