requestOptionsOverrideConstructor.body().invoke(SUPER).arg(originalResourceField).arg(requestOptionsOverrideOptionsParam);
}
else
{
requestOptionsOverrideConstructor.body().assign(baseUriField, originalResourceField);
final JInvocation requestOptionsOverrideAssignRequestOptions = new JBlock().invoke("assignRequestOptions").arg(requestOptionsOverrideOptionsParam);
requestOptionsOverrideConstructor.body().assign(requestOptionsField, requestOptionsOverrideAssignRequestOptions);
}
// primary resource name override constructor, delegates to the main constructor
JVar resourceNameOverrideResourceNameParam = resourceNameOverrideConstructor.param(_stringClass, "primaryResourceName");
resourceNameOverrideConstructor.body().invoke(THIS).arg(resourceNameOverrideResourceNameParam).arg(defaultOptionsField);
// main constructor
JVar mainConsResourceNameParam = mainConstructor.param(_stringClass, "primaryResourceName");
JVar mainConsOptionsParam = mainConstructor.param(RestliRequestOptions.class, "requestOptions");
JExpression baseUriExpr;
if (resourcePath.contains("/"))
{
baseUriExpr = originalResourceField.invoke("replaceFirst").arg(JExpr.lit("[^/]*/")).arg(mainConsResourceNameParam.plus(JExpr.lit("/")));
}
else
{
baseUriExpr = mainConsResourceNameParam;
}
if (_config.isRestli2Format())
{
mainConstructor.body().invoke(SUPER).arg(baseUriExpr).arg(mainConsOptionsParam);
}
else
{
final JInvocation mainAssignRequestOptions = new JBlock().invoke("assignRequestOptions").arg(mainConsOptionsParam);
mainConstructor.body().assign(baseUriField, baseUriExpr);
mainConstructor.body().assign(requestOptionsField, mainAssignRequestOptions);
}
JMethod primaryResourceGetter = facadeClass.method(JMod.PUBLIC | JMod.STATIC, String.class, "getPrimaryResource");
primaryResourceGetter.body()._return(originalResourceField);
List<String> pathKeys = getPathKeys(resourcePath, pathToAssocKeys);
JClass keyTyperefClass = null;
JClass keyClass;
JClass keyKeyClass = null;
JClass keyParamsClass = null;
Class<?> resourceSchemaClass;
Map<String, AssocKeyTypeInfo> assocKeyTypeInfos = Collections.emptyMap();
StringArray supportsList=null;
RestMethodSchemaArray restMethods = null;
FinderSchemaArray finders = null;
ResourceSchemaArray subresources = null;
ActionSchemaArray resourceActions = null;
ActionSchemaArray entityActions = null;
if (resource.getCollection() != null)
{
resourceSchemaClass = CollectionSchema.class;
CollectionSchema collection = resource.getCollection();
String keyName = collection.getIdentifier().getName();
// In case of collection with a simple key, return the one specified by "type" in
// the "identifier". Otherwise, get both "type" and "params", and return
// ComplexKeyResource parameterized by those two.
if (collection.getIdentifier().getParams() == null)
{
keyClass = getJavaBindingType(collection.getIdentifier().getType(), facadeClass).valueClass;
JClass declaredClass = getClassRefForSchema(RestSpecCodec.textToSchema(collection.getIdentifier().getType(), getSchemaResolver()), facadeClass);
if(!declaredClass.equals(keyClass))
{
keyTyperefClass = declaredClass;
}
}
else
{
keyKeyClass = getJavaBindingType(collection.getIdentifier().getType(), facadeClass).valueClass;
keyParamsClass = getJavaBindingType(collection.getIdentifier().getParams(), facadeClass).valueClass;
keyClass = getCodeModel().ref(ComplexResourceKey.class).narrow(keyKeyClass, keyParamsClass);
}
pathKeyTypes.put(keyName, keyClass);
supportsList = collection.getSupports();
restMethods = collection.getMethods();
finders = collection.getFinders();
subresources = collection.getEntity().getSubresources();
resourceActions = collection.getActions();
entityActions = collection.getEntity().getActions();
}
else if (resource.getAssociation() != null)
{
resourceSchemaClass = AssociationSchema.class;
AssociationSchema association = resource.getAssociation();
keyClass = getCodeModel().ref(CompoundKey.class);
supportsList = association.getSupports();
restMethods = association.getMethods();
finders = association.getFinders();
subresources = association.getEntity().getSubresources();
resourceActions = association.getActions();
entityActions = association.getEntity().getActions();
assocKeyTypeInfos = generateAssociationKey(facadeClass, association);
String keyName = getAssociationKey(resource, association);
pathKeyTypes.put(keyName, keyClass);
List<String> assocKeys = new ArrayList<String>(4);
for(Map.Entry<String, AssocKeyTypeInfo> entry: assocKeyTypeInfos.entrySet())
{
assocKeys.add(entry.getKey());
assocKeyTypes.put(entry.getKey(), entry.getValue().getBindingType());
}
pathToAssocKeys.put(keyName, assocKeys);
}
else if (resource.getSimple() != null)
{
resourceSchemaClass = SimpleSchema.class;
SimpleSchema simpleSchema = resource.getSimple();
keyClass = _voidClass;
supportsList = simpleSchema.getSupports();
restMethods = simpleSchema.getMethods();
subresources = simpleSchema.getEntity().getSubresources();
resourceActions = simpleSchema.getActions();
}
else if (resource.getActionsSet() != null)
{
resourceSchemaClass = ActionsSetSchema.class;
ActionsSetSchema actionsSet = resource.getActionsSet();
resourceActions = actionsSet.getActions();
keyClass = _voidClass;
}
else
{
throw new IllegalArgumentException("unsupported resource type for resource: '" + resourceName + '\'');
}
generateOptions(facadeClass, baseUriGetter, requestOptionsGetter);
JFieldVar resourceSpecField = facadeClass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, _resourceSpecClass, "_resourceSpec");
if (resourceSchemaClass == CollectionSchema.class ||
resourceSchemaClass == AssociationSchema.class ||
resourceSchemaClass == SimpleSchema.class)
{
JClass schemaClass = getJavaBindingType(resource.getSchema(), null).schemaClass;
Set<ResourceMethod> supportedMethods = getSupportedMethods(supportsList);
JInvocation supportedMethodsExpr;
if (supportedMethods.isEmpty())
{
supportedMethodsExpr = _enumSetClass.staticInvoke("noneOf").arg(_resourceMethodClass.dotclass());
}
else
{
supportedMethodsExpr = _enumSetClass.staticInvoke("of");
for (ResourceMethod resourceMethod: supportedMethods)
{
validateResourceMethod(resourceSchemaClass, resourceName, resourceMethod);
supportedMethodsExpr.arg(_resourceMethodClass.staticRef(resourceMethod.name()));
}
}
JBlock staticInit = facadeClass.init();
JVar methodSchemaMap = methodMetadataMapInit(facadeClass, resourceActions, entityActions,
staticInit);
JVar responseSchemaMap = responseMetadataMapInit(facadeClass, resourceActions, entityActions,
staticInit);
if (resourceSchemaClass == CollectionSchema.class ||
resourceSchemaClass == AssociationSchema.class)
{
JClass assocKeyClass = getCodeModel().ref(TypeInfo.class);
JClass hashMapClass = getCodeModel().ref(HashMap.class).narrow(_stringClass, assocKeyClass);
JVar keyPartsVar = staticInit.decl(hashMapClass, "keyParts").init(JExpr._new(hashMapClass));
for (Map.Entry<String, AssocKeyTypeInfo> typeInfoEntry : assocKeyTypeInfos.entrySet())
{
AssocKeyTypeInfo typeInfo = typeInfoEntry.getValue();
JInvocation typeArg = JExpr._new(assocKeyClass)
.arg(typeInfo.getBindingType().dotclass())
.arg(typeInfo.getDeclaredType().dotclass());
staticInit.add(keyPartsVar.invoke("put").arg(typeInfoEntry.getKey()).arg(typeArg));
}
staticInit.assign(resourceSpecField,
JExpr._new(_resourceSpecImplClass)
.arg(supportedMethodsExpr)
.arg(methodSchemaMap)
.arg(responseSchemaMap)
.arg(keyTyperefClass == null ? keyClass.dotclass() : keyTyperefClass.dotclass())
.arg(keyKeyClass == null ? JExpr._null() : keyKeyClass.dotclass())
.arg(keyKeyClass == null ? JExpr._null() : keyParamsClass.dotclass())
.arg(schemaClass.dotclass())
.arg(keyPartsVar));
}
else //simple schema
{
staticInit.assign(resourceSpecField,
JExpr._new(_resourceSpecImplClass)
.arg(supportedMethodsExpr)
.arg(methodSchemaMap)
.arg(responseSchemaMap)
.arg(schemaClass.dotclass()));
}
generateBasicMethods(facadeClass,
baseUriGetter,
keyClass,
schemaClass,
supportedMethods,
restMethods,
resourceSpecField,
resourceName,
pathKeys,
pathKeyTypes,
assocKeyTypes,
pathToAssocKeys,
requestOptionsGetter);
if (resourceSchemaClass == CollectionSchema.class ||
resourceSchemaClass == AssociationSchema.class)
{
generateFinders(facadeClass,
baseUriGetter,
finders,
keyClass,
schemaClass,
assocKeyTypeInfos,
resourceSpecField,
resourceName,
pathKeys,
pathKeyTypes,
assocKeyTypes,
pathToAssocKeys,
requestOptionsGetter);
}
generateSubResources(sourceFile, subresources, pathKeyTypes, assocKeyTypes, pathToAssocKeys);
}
else //action set
{
JBlock staticInit = facadeClass.init();
JInvocation supportedMethodsExpr = _enumSetClass.staticInvoke("noneOf").arg(_resourceMethodClass.dotclass());
JVar methodSchemaMap = methodMetadataMapInit(facadeClass, resourceActions, entityActions,
staticInit);
JVar responseSchemaMap = responseMetadataMapInit(facadeClass, resourceActions, entityActions,
staticInit);
staticInit.assign(resourceSpecField,