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;
}
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;
}
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;
}
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);
}
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);
}
Aggregations