All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.apio.architect.sample.internal.router.PersonActionRouter Maven / Gradle / Ivy

There is a newer version: 1.0.16
Show newest version
/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.apio.architect.sample.internal.router;

import static com.liferay.apio.architect.sample.internal.auth.PermissionChecker.hasPermission;
import static com.liferay.apio.architect.sample.internal.converter.PersonConverter.toPerson;

import com.liferay.apio.architect.annotation.Actions;
import com.liferay.apio.architect.annotation.Actions.Create;
import com.liferay.apio.architect.annotation.Actions.Remove;
import com.liferay.apio.architect.annotation.Actions.Replace;
import com.liferay.apio.architect.annotation.Actions.Retrieve;
import com.liferay.apio.architect.annotation.Body;
import com.liferay.apio.architect.annotation.EntryPoint;
import com.liferay.apio.architect.annotation.Id;
import com.liferay.apio.architect.credentials.Credentials;
import com.liferay.apio.architect.pagination.PageItems;
import com.liferay.apio.architect.pagination.Pagination;
import com.liferay.apio.architect.router.ActionRouter;
import com.liferay.apio.architect.sample.internal.converter.PersonConverter;
import com.liferay.apio.architect.sample.internal.dao.PersonModelService;
import com.liferay.apio.architect.sample.internal.dto.PersonModel;
import com.liferay.apio.architect.sample.internal.dto.PostalAddressModel;
import com.liferay.apio.architect.sample.internal.type.Person;
import com.liferay.apio.architect.sample.internal.type.PostalAddress;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.ws.rs.ForbiddenException;
import javax.ws.rs.NotFoundException;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
 * Provides all the information necessary to expose Person resources through a web API.
 *
 * @author Alejandro Hernández
 */
@Component(
	immediate = true, service = {ActionRouter.class, PersonActionRouter.class}
)
public class PersonActionRouter implements ActionRouter {

	@Create
	public Person create(@Body Person person, Credentials credentials) {
		if (!hasPermission(credentials)) {
			throw new ForbiddenException();
		}

		PostalAddress postalAddress = person.getPostalAddress();

		PostalAddressModel postalAddressModel = new PostalAddressModel(
			postalAddress.getAddressCountry(), postalAddress.getAddressRegion(),
			postalAddress.getAddressLocality(), postalAddress.getPostalCode(),
			postalAddress.getStreetAddress());

		PersonModel personModel = _personModelService.create(
			postalAddressModel, person.getImage(), person.getBirthDate(),
			person.getEmail(), person.getGivenName(), person.getJobTitles(),
			person.getFamilyName());

		return toPerson(personModel);
	}

	@Actions.Action(httpMethod = "GET", name = "recover_first_user")
	public Person recoverFirstUser() {
		List personModels = _personModelService.getPage(0, 1);

		PersonModel personModel = personModels.get(0);

		return PersonConverter.toPerson(personModel);
	}

	@Remove
	public void remove(@Id long id, Credentials credentials) {
		if (!hasPermission(credentials)) {
			throw new ForbiddenException();
		}

		_personModelService.remove(id);
	}

	@Replace
	public Person replace(
		@Id long id, @Body Person person, Credentials credentials) {

		if (!hasPermission(credentials)) {
			throw new ForbiddenException();
		}

		PostalAddress postalAddress = person.getPostalAddress();

		PostalAddressModel postalAddressModel = new PostalAddressModel(
			postalAddress.getAddressCountry(), postalAddress.getAddressRegion(),
			postalAddress.getAddressLocality(), postalAddress.getPostalCode(),
			postalAddress.getStreetAddress());

		Optional optional = _personModelService.update(
			postalAddressModel, person.getImage(), person.getBirthDate(),
			person.getEmail(), person.getGivenName(), person.getJobTitles(),
			person.getFamilyName(), id);

		return optional.map(
			PersonConverter::toPerson
		).orElseThrow(
			() -> new NotFoundException("Unable to get person " + id)
		);
	}

	@Retrieve
	public Person retrieve(@Id long id) {
		Optional optional = _personModelService.get(id);

		return optional.map(
			PersonConverter::toPerson
		).orElseThrow(
			() -> new NotFoundException("Unable to get person " + id)
		);
	}

	@EntryPoint
	@Retrieve
	public PageItems retrieveCollection(Pagination pagination) {
		List personModels = _personModelService.getPage(
			pagination.getStartPosition(), pagination.getEndPosition());
		int count = _personModelService.getCount();

		Stream stream = personModels.stream();

		List persons = stream.map(
			PersonConverter::toPerson
		).collect(
			Collectors.toList()
		);

		return new PageItems<>(persons, count);
	}

	@Reference
	private PersonModelService _personModelService;

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy