use of com.liferay.apio.architect.message.json.JSONObjectBuilder in project com-liferay-apio-architect by liferay.
the class SingleModelWriter method writeEmbeddedModelFields.
/**
* Writes a related {@link SingleModel} with the {@link
* SingleModelMessageMapper}. This method uses a {@link FieldsWriter} to
* write the different fields of its {@link Representor}. If no {@code
* Representor} or {@link com.liferay.apio.architect.uri.Path} exists for
* the model, this method doesn't do anything.
*
* @param singleModel the {@code SingleModel} to write
* @param embeddedPathElements the related model's embedded path elements
*/
public <S> void writeEmbeddedModelFields(SingleModel<S> singleModel, FunctionalList<String> embeddedPathElements, RepresentorFunction representorFunction) {
Optional<FieldsWriter<S, ?>> optional = getFieldsWriter(singleModel, embeddedPathElements, _requestInfo, _pathFunction, representorFunction, _representorFunction, _singleModelFunction, _singleModel);
if (!optional.isPresent()) {
return;
}
FieldsWriter<S, ?> fieldsWriter = optional.get();
fieldsWriter.writeBooleanFields((field, value) -> _singleModelMessageMapper.mapEmbeddedResourceBooleanField(_jsonObjectBuilder, embeddedPathElements, field, value));
fieldsWriter.writeBooleanListFields((field, value) -> _singleModelMessageMapper.mapEmbeddedResourceBooleanListField(_jsonObjectBuilder, embeddedPathElements, field, value));
fieldsWriter.writeLocalizedStringFields((field, value) -> _singleModelMessageMapper.mapEmbeddedResourceStringField(_jsonObjectBuilder, embeddedPathElements, field, value));
fieldsWriter.writeNumberFields((field, value) -> _singleModelMessageMapper.mapEmbeddedResourceNumberField(_jsonObjectBuilder, embeddedPathElements, field, value));
fieldsWriter.writeNumberListFields((field, value) -> _singleModelMessageMapper.mapEmbeddedResourceNumberListField(_jsonObjectBuilder, embeddedPathElements, field, value));
fieldsWriter.writeStringFields((field, value) -> _singleModelMessageMapper.mapEmbeddedResourceStringField(_jsonObjectBuilder, embeddedPathElements, field, value));
fieldsWriter.writeStringListFields((field, value) -> _singleModelMessageMapper.mapEmbeddedResourceStringListField(_jsonObjectBuilder, embeddedPathElements, field, value));
fieldsWriter.writeLinks((fieldName, link) -> _singleModelMessageMapper.mapEmbeddedResourceLink(_jsonObjectBuilder, embeddedPathElements, fieldName, link));
fieldsWriter.writeTypes(types -> _singleModelMessageMapper.mapEmbeddedResourceTypes(_jsonObjectBuilder, embeddedPathElements, types));
fieldsWriter.writeBinaries((field, value) -> _singleModelMessageMapper.mapEmbeddedResourceLink(_jsonObjectBuilder, embeddedPathElements, field, value));
List<Operation> operations = singleModel.getOperations();
operations.forEach(operation -> {
JSONObjectBuilder operationJSONObjectBuilder = new JSONObjectBuilder();
_singleModelMessageMapper.onStartEmbeddedOperation(_jsonObjectBuilder, operationJSONObjectBuilder, embeddedPathElements, operation);
Optional<Form> formOptional = operation.getFormOptional();
formOptional.ifPresent(form -> {
String url = createFormURL(_requestInfo.getServerURL(), form);
_singleModelMessageMapper.mapEmbeddedOperationFormURL(_jsonObjectBuilder, operationJSONObjectBuilder, embeddedPathElements, url);
});
_singleModelMessageMapper.mapEmbeddedOperationMethod(_jsonObjectBuilder, operationJSONObjectBuilder, embeddedPathElements, operation.method);
_singleModelMessageMapper.onFinishEmbeddedOperation(_jsonObjectBuilder, operationJSONObjectBuilder, embeddedPathElements, operation);
});
fieldsWriter.writeRelatedModels(embeddedSingleModel -> getPathOptional(embeddedSingleModel, _pathFunction, _representorFunction), this::writeEmbeddedModelFields, (resourceURL, resourceEmbeddedPathElements) -> _singleModelMessageMapper.mapLinkedResourceURL(_jsonObjectBuilder, resourceEmbeddedPathElements, resourceURL), (resourceURL, resourceEmbeddedPathElements) -> _singleModelMessageMapper.mapEmbeddedResourceURL(_jsonObjectBuilder, resourceEmbeddedPathElements, resourceURL));
fieldsWriter.writeRelatedCollections(_resourceNameFunction, (url, resourceEmbeddedPathElements) -> _singleModelMessageMapper.mapLinkedResourceURL(_jsonObjectBuilder, resourceEmbeddedPathElements, url));
_writeNestedResources(representorFunction, singleModel, embeddedPathElements);
}
use of com.liferay.apio.architect.message.json.JSONObjectBuilder in project com-liferay-apio-architect by liferay.
the class SingleModelWriter method write.
/**
* Writes the handled {@link SingleModel} to a string. This method uses a
* {@link FieldsWriter} to write the different fields of its {@link
* Representor}. If no {@code Representor} or {@code Path} exists for the
* model, this method returns {@code Optional#empty()}.
*
* @return the string representation of the {@code SingleModel}, if the
* model's {@code Representor} and {@code Path} exist; returns
* {@code Optional#empty()} otherwise
*/
@SuppressWarnings("Duplicates")
public Optional<String> write() {
Optional<FieldsWriter<T, ?>> optional = getFieldsWriter(_singleModel, null, _requestInfo, _pathFunction, _representorFunction, _singleModelFunction);
if (!optional.isPresent()) {
return Optional.empty();
}
FieldsWriter<T, ?> fieldsWriter = optional.get();
_singleModelMessageMapper.onStart(_jsonObjectBuilder, _singleModel, _requestInfo.getHttpHeaders());
fieldsWriter.writeBooleanFields((field, value) -> _singleModelMessageMapper.mapBooleanField(_jsonObjectBuilder, field, value));
fieldsWriter.writeBooleanListFields((field, value) -> _singleModelMessageMapper.mapBooleanListField(_jsonObjectBuilder, field, value));
fieldsWriter.writeLocalizedStringFields((field, value) -> _singleModelMessageMapper.mapStringField(_jsonObjectBuilder, field, value));
fieldsWriter.writeNumberFields((field, value) -> _singleModelMessageMapper.mapNumberField(_jsonObjectBuilder, field, value));
fieldsWriter.writeNumberListFields((field, value) -> _singleModelMessageMapper.mapNumberListField(_jsonObjectBuilder, field, value));
fieldsWriter.writeStringFields((field, value) -> _singleModelMessageMapper.mapStringField(_jsonObjectBuilder, field, value));
fieldsWriter.writeStringListFields((field, value) -> _singleModelMessageMapper.mapStringListField(_jsonObjectBuilder, field, value));
fieldsWriter.writeLinks((fieldName, link) -> _singleModelMessageMapper.mapLink(_jsonObjectBuilder, fieldName, link));
fieldsWriter.writeTypes(types -> _singleModelMessageMapper.mapTypes(_jsonObjectBuilder, types));
fieldsWriter.writeBinaries((field, value) -> _singleModelMessageMapper.mapLink(_jsonObjectBuilder, field, value));
fieldsWriter.writeSingleURL(url -> _singleModelMessageMapper.mapSelfURL(_jsonObjectBuilder, url));
List<Operation> operations = _singleModel.getOperations();
operations.forEach(operation -> {
JSONObjectBuilder operationJSONObjectBuilder = new JSONObjectBuilder();
_singleModelMessageMapper.onStartOperation(_jsonObjectBuilder, operationJSONObjectBuilder, operation);
Optional<Form> formOptional = operation.getFormOptional();
formOptional.map(form -> createFormURL(_requestInfo.getServerURL(), form)).ifPresent(url -> _singleModelMessageMapper.mapOperationFormURL(_jsonObjectBuilder, operationJSONObjectBuilder, url));
_singleModelMessageMapper.mapOperationMethod(_jsonObjectBuilder, operationJSONObjectBuilder, operation.method);
_singleModelMessageMapper.onFinishOperation(_jsonObjectBuilder, operationJSONObjectBuilder, operation);
});
fieldsWriter.writeRelatedModels(singleModel -> getPathOptional(singleModel, _pathFunction, _representorFunction), this::writeEmbeddedModelFields, (resourceURL, embeddedPathElements) -> _singleModelMessageMapper.mapLinkedResourceURL(_jsonObjectBuilder, embeddedPathElements, resourceURL), (resourceURL, embeddedPathElements) -> _singleModelMessageMapper.mapEmbeddedResourceURL(_jsonObjectBuilder, embeddedPathElements, resourceURL));
fieldsWriter.writeRelatedCollections(_resourceNameFunction, (url, embeddedPathElements) -> _singleModelMessageMapper.mapLinkedResourceURL(_jsonObjectBuilder, embeddedPathElements, url));
_writeNestedResources(_representorFunction, _singleModel, null);
_singleModelMessageMapper.onFinish(_jsonObjectBuilder, _singleModel, _requestInfo.getHttpHeaders());
JsonObject jsonObject = _jsonObjectBuilder.build();
return Optional.of(jsonObject.toString());
}
use of com.liferay.apio.architect.message.json.JSONObjectBuilder in project com-liferay-apio-architect by liferay.
the class DocumentationWriter method write.
/**
* Writes the {@link Documentation} to a string.
*
* @return the JSON representation of the {@code Documentation}
*/
public String write() {
JSONObjectBuilder jsonObjectBuilder = new JSONObjectBuilder();
RequestFunction<Optional<String>> apiTitleRequestFunction = _documentation.getAPITitleRequestFunction();
apiTitleRequestFunction.apply(_requestInfo.getHttpServletRequest()).ifPresent(title -> _documentationMessageMapper.mapTitle(jsonObjectBuilder, title));
RequestFunction<Optional<String>> apiDescriptionRequestFunction = _documentation.getAPIDescriptionRequestFunction();
apiDescriptionRequestFunction.apply(_requestInfo.getHttpServletRequest()).ifPresent(description -> _documentationMessageMapper.mapDescription(jsonObjectBuilder, description));
_documentationMessageMapper.onStart(jsonObjectBuilder, _documentation, _requestInfo.getHttpHeaders());
_documentationMessageMapper.onFinish(jsonObjectBuilder, _documentation, _requestInfo.getHttpHeaders());
JsonObject jsonObject = jsonObjectBuilder.build();
return jsonObject.toString();
}
use of com.liferay.apio.architect.message.json.JSONObjectBuilder in project com-liferay-apio-architect by liferay.
the class PageWriter method _writeItem.
private void _writeItem(SingleModel<T> singleModel) {
Optional<FieldsWriter<T, ?>> optional = getFieldsWriter(singleModel, null, _requestInfo, _pathFunction, _representorFunction, _singleModelFunction);
if (!optional.isPresent()) {
return;
}
FieldsWriter<T, ?> fieldsWriter = optional.get();
JSONObjectBuilder itemJsonObjectBuilder = new JSONObjectBuilder();
_pageMessageMapper.onStartItem(_jsonObjectBuilder, itemJsonObjectBuilder, singleModel, _requestInfo.getHttpHeaders());
fieldsWriter.writeBooleanFields((field, value) -> _pageMessageMapper.mapItemBooleanField(_jsonObjectBuilder, itemJsonObjectBuilder, field, value));
fieldsWriter.writeBooleanListFields((field, value) -> _pageMessageMapper.mapItemBooleanListField(_jsonObjectBuilder, itemJsonObjectBuilder, field, value));
fieldsWriter.writeLocalizedStringFields((field, value) -> _pageMessageMapper.mapItemStringField(_jsonObjectBuilder, itemJsonObjectBuilder, field, value));
fieldsWriter.writeNumberFields((field, value) -> _pageMessageMapper.mapItemNumberField(_jsonObjectBuilder, itemJsonObjectBuilder, field, value));
fieldsWriter.writeNumberListFields((field, value) -> _pageMessageMapper.mapItemNumberListField(_jsonObjectBuilder, itemJsonObjectBuilder, field, value));
fieldsWriter.writeStringFields((field, value) -> _pageMessageMapper.mapItemStringField(_jsonObjectBuilder, itemJsonObjectBuilder, field, value));
fieldsWriter.writeStringListFields((field, value) -> _pageMessageMapper.mapItemStringListField(_jsonObjectBuilder, itemJsonObjectBuilder, field, value));
fieldsWriter.writeLinks((fieldName, link) -> _pageMessageMapper.mapItemLink(_jsonObjectBuilder, itemJsonObjectBuilder, fieldName, link));
fieldsWriter.writeTypes(types -> _pageMessageMapper.mapItemTypes(_jsonObjectBuilder, itemJsonObjectBuilder, types));
fieldsWriter.writeBinaries((field, value) -> _pageMessageMapper.mapItemLink(_jsonObjectBuilder, itemJsonObjectBuilder, field, value));
fieldsWriter.writeSingleURL(url -> _pageMessageMapper.mapItemSelfURL(_jsonObjectBuilder, itemJsonObjectBuilder, url));
fieldsWriter.writeRelatedModels(embeddedSingleModel -> getPathOptional(embeddedSingleModel, _pathFunction, _representorFunction), (embeddedSingleModel, embeddedPathElements1) -> _writeItemEmbeddedModelFields(embeddedSingleModel, embeddedPathElements1, itemJsonObjectBuilder), (resourceURL, embeddedPathElements) -> _pageMessageMapper.mapItemLinkedResourceURL(_jsonObjectBuilder, itemJsonObjectBuilder, embeddedPathElements, resourceURL), (resourceURL, embeddedPathElements) -> _pageMessageMapper.mapItemEmbeddedResourceURL(_jsonObjectBuilder, itemJsonObjectBuilder, embeddedPathElements, resourceURL));
fieldsWriter.writeRelatedCollections(_resourceNameFunction, (url, embeddedPathElements) -> _pageMessageMapper.mapItemLinkedResourceURL(_jsonObjectBuilder, itemJsonObjectBuilder, embeddedPathElements, url));
_writeNestedResources(_representorFunction, singleModel, itemJsonObjectBuilder, singleModel, null);
_pageMessageMapper.onFinishItem(_jsonObjectBuilder, itemJsonObjectBuilder, singleModel, _requestInfo.getHttpHeaders());
}
use of com.liferay.apio.architect.message.json.JSONObjectBuilder in project com-liferay-apio-architect by liferay.
the class PageWriter method write.
/**
* Writes the handled {@link Page} to a string. This method uses a {@link
* FieldsWriter} to write the different fields of its items' {@link
* Representor}. If no {@code Representor} or {@code Path} exist for the
* model, this method returns {@code Optional#empty()}.
*
* @return the representation of the {@code Page}, if the {@code
* Representor} and {@code Path} exist for the model; returns {@code
* Optional#empty()} otherwise
*/
@SuppressWarnings("Duplicates")
public String write() {
_pageMessageMapper.onStart(_jsonObjectBuilder, _page, _requestInfo.getHttpHeaders());
_pageMessageMapper.mapItemTotalCount(_jsonObjectBuilder, _page.getTotalCount());
Collection<T> items = _page.getItems();
_pageMessageMapper.mapPageCount(_jsonObjectBuilder, items.size());
_writePageURLs();
String url = _getCollectionURL();
_pageMessageMapper.mapCollectionURL(_jsonObjectBuilder, url);
String resourceName = _page.getResourceName();
items.forEach(model -> _writeItem(new SingleModel<>(model, resourceName, Collections.emptyList())));
List<Operation> operations = _page.getOperations();
operations.forEach(operation -> {
JSONObjectBuilder operationJSONObjectBuilder = new JSONObjectBuilder();
_pageMessageMapper.onStartOperation(_jsonObjectBuilder, operationJSONObjectBuilder, operation);
Optional<Form> formOptional = operation.getFormOptional();
formOptional.map(form -> createFormURL(_requestInfo.getServerURL(), form)).ifPresent(formURL -> _pageMessageMapper.mapOperationFormURL(_jsonObjectBuilder, operationJSONObjectBuilder, formURL));
_pageMessageMapper.mapOperationMethod(_jsonObjectBuilder, operationJSONObjectBuilder, operation.method);
_pageMessageMapper.onFinishOperation(_jsonObjectBuilder, operationJSONObjectBuilder, operation);
});
_pageMessageMapper.onFinish(_jsonObjectBuilder, _page, _requestInfo.getHttpHeaders());
JsonObject jsonObject = _jsonObjectBuilder.build();
return jsonObject.toString();
}
Aggregations