Search in sources :

Example 1 with Representor

use of com.liferay.apio.architect.representor.Representor in project com-liferay-apio-architect by liferay.

the class MockRepresentorCreator method createRootModelRepresentor.

/**
 * Creates a mock {@code Representor} for {@code RootModel}.
 *
 * @param  activateNulls whether to add {@code null} empty values
 * @return the mock {@code Representor} for {@code RootModel}
 */
public static Representor<RootModel, String> createRootModelRepresentor(boolean activateNulls) {
    Representor.Builder<RootModel, String> builder = new Representor.Builder<>(RootModelId.class);
    Representor.Builder<RootModel, String>.FirstStep firstStepBuilder = builder.types("Type 1", "Type 2").identifier(RootModel::getId).addBinary("binary1", __ -> null).addBinary("binary2", __ -> null).addBoolean("boolean1", __ -> true).addBoolean("boolean2", __ -> false).addBooleanList("booleanList1", __ -> asList(true, true, false, false)).addBooleanList("booleanList2", __ -> asList(true, false, true, false)).addDate("date1", __ -> new Date(1465981200000L)).addDate("date2", __ -> new Date(1491244560000L)).addLinkedModel("embedded1", FirstEmbeddedId.class, __ -> "first").addLinkedModel("embedded2", FirstEmbeddedId.class, __ -> "second").addLinkedModel("linked1", FirstEmbeddedId.class, __ -> "third").addLinkedModel("linked2", FirstEmbeddedId.class, __ -> "fourth").addLink("link1", "www.liferay.com").addLink("link2", "community.liferay.com").addLocalizedStringByLanguage("localizedString1", (model, language) -> "Translated 1").addLocalizedStringByLanguage("localizedString2", (model, language) -> "Translated 2").addNumber("number1", __ -> 2017).addNumber("number2", __ -> 42).addNumberList("numberList1", __ -> asList(1, 2, 3, 4, 5)).addNumberList("numberList2", __ -> asList(6, 7, 8, 9, 10)).addRelatedCollection("relatedCollection1", FirstEmbeddedId.class).addRelatedCollection("relatedCollection2", FirstEmbeddedId.class).addString("string1", __ -> "Live long and prosper").addString("string2", __ -> "Hypermedia").addStringList("stringList1", __ -> asList("a", "b", "c", "d", "e")).addStringList("stringList2", __ -> asList("f", "g", "h", "i", "j")).addNested("nested1", __ -> (FirstEmbeddedModel) () -> "id 1", nestedBuilder -> nestedBuilder.nestedTypes("Type 3").addNumber("number1", __ -> 2017).addString("string1", FirstEmbeddedModel::getId).addString("string2", __ -> "string2").build()).addNested("nested2", rootModel -> (SecondEmbeddedModel) rootModel::getId, nestedBuilder -> nestedBuilder.nestedTypes("Type 4").addBidirectionalModel("bidirectionalModel3", "bidirectionalKey", FirstEmbeddedId.class, (Function<SecondEmbeddedModel, String>) SecondEmbeddedModel::getId).addString("string1", SecondEmbeddedModel::getId).addNumber("number1", __ -> 42).addLinkedModel("linked3", ThirdEmbeddedId.class, __ -> "fifth").addNested("nested3", __ -> () -> "id 3", (Representor.Builder<ThirdEmbeddedModel, ?> thirdEmbeddedModelBuilder) -> thirdEmbeddedModelBuilder.nestedTypes("Type 5").addString("string1", ThirdEmbeddedModel::getId).build()).addNumber("number1", __ -> 42).addRelatedCollection("relatedCollection3", ThirdEmbeddedId.class).addString("string1", SecondEmbeddedModel::getId).build());
    if (activateNulls) {
        return firstStepBuilder.addBoolean("boolean3", __ -> null).addLink("link3", null).addLink("link4", "").addLocalizedStringByLanguage("localizedString3", (model, language) -> null).addLocalizedStringByLanguage("localizedString4", (model, language) -> "").addNumber("number3", __ -> null).addString("string3", __ -> null).addString("string4", __ -> "").build();
    }
    return firstStepBuilder.build();
}
Also used : Date(java.util.Date) RootModelId(com.liferay.apio.architect.test.util.identifier.RootModelId) Function(java.util.function.Function) Collections.singletonList(java.util.Collections.singletonList) Representor(com.liferay.apio.architect.representor.Representor) SecondEmbeddedId(com.liferay.apio.architect.test.util.identifier.SecondEmbeddedId) SecondEmbeddedModel(com.liferay.apio.architect.test.util.model.SecondEmbeddedModel) Arrays.asList(java.util.Arrays.asList) RootModel(com.liferay.apio.architect.test.util.model.RootModel) FirstEmbeddedId(com.liferay.apio.architect.test.util.identifier.FirstEmbeddedId) FirstEmbeddedModel(com.liferay.apio.architect.test.util.model.FirstEmbeddedModel) ThirdEmbeddedId(com.liferay.apio.architect.test.util.identifier.ThirdEmbeddedId) ThirdEmbeddedModel(com.liferay.apio.architect.test.util.model.ThirdEmbeddedModel) RootModel(com.liferay.apio.architect.test.util.model.RootModel) FirstEmbeddedModel(com.liferay.apio.architect.test.util.model.FirstEmbeddedModel) Representor(com.liferay.apio.architect.representor.Representor) Date(java.util.Date) Function(java.util.function.Function) SecondEmbeddedModel(com.liferay.apio.architect.test.util.model.SecondEmbeddedModel) ThirdEmbeddedId(com.liferay.apio.architect.test.util.identifier.ThirdEmbeddedId) ThirdEmbeddedModel(com.liferay.apio.architect.test.util.model.ThirdEmbeddedModel) FirstEmbeddedId(com.liferay.apio.architect.test.util.identifier.FirstEmbeddedId)

Example 2 with Representor

use of com.liferay.apio.architect.representor.Representor 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());
}
Also used : JsonObject(com.google.gson.JsonObject) ResourceNameFunction(com.liferay.apio.architect.writer.alias.ResourceNameFunction) RepresentorFunction(com.liferay.apio.architect.writer.alias.RepresentorFunction) RequestInfo(com.liferay.apio.architect.request.RequestInfo) Form(com.liferay.apio.architect.form.Form) SingleModelMessageMapper(com.liferay.apio.architect.message.json.SingleModelMessageMapper) WriterUtil.getFieldsWriter(com.liferay.apio.architect.writer.util.WriterUtil.getFieldsWriter) SingleModelFunction(com.liferay.apio.architect.writer.alias.SingleModelFunction) Function(java.util.function.Function) Operation(com.liferay.apio.architect.operation.Operation) Representor(com.liferay.apio.architect.representor.Representor) JSONObjectBuilder(com.liferay.apio.architect.message.json.JSONObjectBuilder) List(java.util.List) Unsafe.unsafeCast(com.liferay.apio.architect.unsafe.Unsafe.unsafeCast) WriterUtil.getPathOptional(com.liferay.apio.architect.writer.util.WriterUtil.getPathOptional) FunctionalList(com.liferay.apio.architect.list.FunctionalList) Map(java.util.Map) Optional(java.util.Optional) SingleModel(com.liferay.apio.architect.single.model.SingleModel) PathFunction(com.liferay.apio.architect.writer.alias.PathFunction) Collections(java.util.Collections) URLCreator.createFormURL(com.liferay.apio.architect.writer.url.URLCreator.createFormURL) JSONObjectBuilder(com.liferay.apio.architect.message.json.JSONObjectBuilder) WriterUtil.getFieldsWriter(com.liferay.apio.architect.writer.util.WriterUtil.getFieldsWriter) Form(com.liferay.apio.architect.form.Form) JsonObject(com.google.gson.JsonObject) Operation(com.liferay.apio.architect.operation.Operation)

Example 3 with Representor

use of com.liferay.apio.architect.representor.Representor 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();
}
Also used : SingleModel(com.liferay.apio.architect.single.model.SingleModel) JsonObject(com.google.gson.JsonObject) ResourceNameFunction(com.liferay.apio.architect.writer.alias.ResourceNameFunction) RequestInfo(com.liferay.apio.architect.request.RequestInfo) URLCreator.createCollectionPageURL(com.liferay.apio.architect.writer.url.URLCreator.createCollectionPageURL) WriterUtil.getFieldsWriter(com.liferay.apio.architect.writer.util.WriterUtil.getFieldsWriter) SingleModelFunction(com.liferay.apio.architect.writer.alias.SingleModelFunction) Function(java.util.function.Function) URLCreator.createCollectionURL(com.liferay.apio.architect.writer.url.URLCreator.createCollectionURL) Operation(com.liferay.apio.architect.operation.Operation) Representor(com.liferay.apio.architect.representor.Representor) WriterUtil.getPathOptional(com.liferay.apio.architect.writer.util.WriterUtil.getPathOptional) FunctionalList(com.liferay.apio.architect.list.FunctionalList) Page(com.liferay.apio.architect.pagination.Page) Map(java.util.Map) PageType(com.liferay.apio.architect.pagination.PageType) SingleModel(com.liferay.apio.architect.single.model.SingleModel) PathFunction(com.liferay.apio.architect.writer.alias.PathFunction) RepresentorFunction(com.liferay.apio.architect.writer.alias.RepresentorFunction) Collection(java.util.Collection) Form(com.liferay.apio.architect.form.Form) Path(com.liferay.apio.architect.uri.Path) JSONObjectBuilder(com.liferay.apio.architect.message.json.JSONObjectBuilder) PageMessageMapper(com.liferay.apio.architect.message.json.PageMessageMapper) List(java.util.List) URLCreator.createNestedCollectionURL(com.liferay.apio.architect.writer.url.URLCreator.createNestedCollectionURL) Unsafe.unsafeCast(com.liferay.apio.architect.unsafe.Unsafe.unsafeCast) Optional(java.util.Optional) Collections(java.util.Collections) URLCreator.createFormURL(com.liferay.apio.architect.writer.url.URLCreator.createFormURL) JSONObjectBuilder(com.liferay.apio.architect.message.json.JSONObjectBuilder) Form(com.liferay.apio.architect.form.Form) JsonObject(com.google.gson.JsonObject) Operation(com.liferay.apio.architect.operation.Operation)

Example 4 with Representor

use of com.liferay.apio.architect.representor.Representor in project com-liferay-apio-architect by liferay.

the class PageWriter method _writeNestedResources.

private <S, U> void _writeNestedResources(RepresentorFunction representorFunction, SingleModel<U> singleModel, JSONObjectBuilder itemJsonObjectBuilder, SingleModel<S> rootSingleModel, FunctionalList<String> embeddedPathElements) {
    Optional<Representor<U, ?>> representorOptional = unsafeCast(representorFunction.apply(singleModel.getResourceName()));
    representorOptional.ifPresent(_representor -> {
        Map<String, Representor<?, ?>> nested = _representor.getNested();
        nested.forEach((key, value) -> {
            Map<String, Function<U, ?>> nestedFunctions = _representor.getNestedFunctions();
            Function<U, ?> nestedMapper = nestedFunctions.get(key);
            Object mappedModel = nestedMapper.apply(singleModel.getModel());
            FunctionalList<String> embeddedNestedPathElements = new FunctionalList<>(embeddedPathElements, key);
            _writeItemEmbeddedModelFields(new SingleModel<>(mappedModel, "", Collections.emptyList()), embeddedNestedPathElements, itemJsonObjectBuilder, __ -> Optional.of(value), rootSingleModel);
        });
    });
}
Also used : ResourceNameFunction(com.liferay.apio.architect.writer.alias.ResourceNameFunction) SingleModelFunction(com.liferay.apio.architect.writer.alias.SingleModelFunction) Function(java.util.function.Function) PathFunction(com.liferay.apio.architect.writer.alias.PathFunction) RepresentorFunction(com.liferay.apio.architect.writer.alias.RepresentorFunction) FunctionalList(com.liferay.apio.architect.list.FunctionalList) JsonObject(com.google.gson.JsonObject) Representor(com.liferay.apio.architect.representor.Representor)

Example 5 with Representor

use of com.liferay.apio.architect.representor.Representor in project com-liferay-apio-architect by liferay.

the class SingleModelWriter method _writeNestedResources.

private <S> void _writeNestedResources(RepresentorFunction representorFunction, SingleModel<S> singleModel, FunctionalList<String> embeddedPathElements) {
    Optional<Representor<S, ?>> representorOptional = unsafeCast(representorFunction.apply(singleModel.getResourceName()));
    representorOptional.ifPresent(_representor -> {
        Map<String, Representor<?, ?>> nested = _representor.getNested();
        nested.forEach((key, value) -> {
            Map<String, Function<S, ?>> nestedFunctions = _representor.getNestedFunctions();
            Function<S, ?> nestedMapper = nestedFunctions.get(key);
            Object mappedModel = nestedMapper.apply(singleModel.getModel());
            FunctionalList<String> embeddedNestedPathElements = new FunctionalList<>(embeddedPathElements, key);
            writeEmbeddedModelFields(new SingleModel<>(mappedModel, "", Collections.emptyList()), embeddedNestedPathElements, __ -> Optional.of(value));
        });
    });
}
Also used : ResourceNameFunction(com.liferay.apio.architect.writer.alias.ResourceNameFunction) RepresentorFunction(com.liferay.apio.architect.writer.alias.RepresentorFunction) SingleModelFunction(com.liferay.apio.architect.writer.alias.SingleModelFunction) Function(java.util.function.Function) PathFunction(com.liferay.apio.architect.writer.alias.PathFunction) FunctionalList(com.liferay.apio.architect.list.FunctionalList) JsonObject(com.google.gson.JsonObject) Representor(com.liferay.apio.architect.representor.Representor)

Aggregations

Representor (com.liferay.apio.architect.representor.Representor)5 Function (java.util.function.Function)5 JsonObject (com.google.gson.JsonObject)4 FunctionalList (com.liferay.apio.architect.list.FunctionalList)4 PathFunction (com.liferay.apio.architect.writer.alias.PathFunction)4 RepresentorFunction (com.liferay.apio.architect.writer.alias.RepresentorFunction)4 ResourceNameFunction (com.liferay.apio.architect.writer.alias.ResourceNameFunction)4 SingleModelFunction (com.liferay.apio.architect.writer.alias.SingleModelFunction)4 Form (com.liferay.apio.architect.form.Form)2 JSONObjectBuilder (com.liferay.apio.architect.message.json.JSONObjectBuilder)2 Operation (com.liferay.apio.architect.operation.Operation)2 RequestInfo (com.liferay.apio.architect.request.RequestInfo)2 SingleModel (com.liferay.apio.architect.single.model.SingleModel)2 Unsafe.unsafeCast (com.liferay.apio.architect.unsafe.Unsafe.unsafeCast)2 URLCreator.createFormURL (com.liferay.apio.architect.writer.url.URLCreator.createFormURL)2 WriterUtil.getFieldsWriter (com.liferay.apio.architect.writer.util.WriterUtil.getFieldsWriter)2 WriterUtil.getPathOptional (com.liferay.apio.architect.writer.util.WriterUtil.getPathOptional)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2