From 664a25ec8dbccf5564071319c9124d0da80ae822 Mon Sep 17 00:00:00 2001 From: Manuel Rego Casasnovas Date: Mon, 22 Mar 2010 12:01:32 +0100 Subject: [PATCH] ItEr50S13AdaptacionServiciosRESTItEr49S18: Basic structure for the new calendars service. --- .../calendars/daos/BaseCalendarDAO.java | 4 +- .../calendars/daos/IBaseCalendarDAO.java | 5 +- .../calendars/entities/BaseCalendar.java | 14 ++- .../calendars/entities/Calendars.hbm.xml | 2 + .../ws/calendars/api/BaseCalendarDTO.java | 57 ++++++++++ .../ws/calendars/api/BaseCalendarListDTO.java | 48 +++++++++ .../ws/calendars/api/ICalendarService.java | 34 ++++++ .../ws/calendars/api/package-info.java | 29 +++++ .../ws/calendars/impl/CalendarConverter.java | 41 +++++++ .../calendars/impl/CalendarServiceREST.java | 87 +++++++++++++++ .../navalplanner-webapp-spring-config.xml | 1 + .../ws/labels/api/CalendarServiceTest.java | 101 ++++++++++++++++++ 12 files changed, 414 insertions(+), 9 deletions(-) create mode 100644 navalplanner-webapp/src/main/java/org/navalplanner/ws/calendars/api/BaseCalendarDTO.java create mode 100644 navalplanner-webapp/src/main/java/org/navalplanner/ws/calendars/api/BaseCalendarListDTO.java create mode 100644 navalplanner-webapp/src/main/java/org/navalplanner/ws/calendars/api/ICalendarService.java create mode 100644 navalplanner-webapp/src/main/java/org/navalplanner/ws/calendars/api/package-info.java create mode 100644 navalplanner-webapp/src/main/java/org/navalplanner/ws/calendars/impl/CalendarConverter.java create mode 100644 navalplanner-webapp/src/main/java/org/navalplanner/ws/calendars/impl/CalendarServiceREST.java create mode 100644 navalplanner-webapp/src/test/java/org/navalplanner/web/test/ws/labels/api/CalendarServiceTest.java diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/calendars/daos/BaseCalendarDAO.java b/navalplanner-business/src/main/java/org/navalplanner/business/calendars/daos/BaseCalendarDAO.java index 7ab0f5ac1..31fc8cc73 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/calendars/daos/BaseCalendarDAO.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/calendars/daos/BaseCalendarDAO.java @@ -29,7 +29,7 @@ import org.hibernate.Criteria; import org.hibernate.criterion.Restrictions; import org.navalplanner.business.calendars.entities.BaseCalendar; import org.navalplanner.business.calendars.entities.ResourceCalendar; -import org.navalplanner.business.common.daos.GenericDAOHibernate; +import org.navalplanner.business.common.daos.IntegrationEntityDAO; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Repository; @@ -44,7 +44,7 @@ import org.springframework.transaction.annotation.Transactional; */ @Repository @Scope(BeanDefinition.SCOPE_SINGLETON) -public class BaseCalendarDAO extends GenericDAOHibernate +public class BaseCalendarDAO extends IntegrationEntityDAO implements IBaseCalendarDAO { @Override diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/calendars/daos/IBaseCalendarDAO.java b/navalplanner-business/src/main/java/org/navalplanner/business/calendars/daos/IBaseCalendarDAO.java index a920a5da0..2a6dbae04 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/calendars/daos/IBaseCalendarDAO.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/calendars/daos/IBaseCalendarDAO.java @@ -23,14 +23,15 @@ package org.navalplanner.business.calendars.daos; import java.util.List; import org.navalplanner.business.calendars.entities.BaseCalendar; -import org.navalplanner.business.common.daos.IGenericDAO; +import org.navalplanner.business.common.daos.IIntegrationEntityDAO; /** * Contract for {@link BaseCalendarDAO} * * @author Manuel Rego Casasnovas */ -public interface IBaseCalendarDAO extends IGenericDAO { +public interface IBaseCalendarDAO extends + IIntegrationEntityDAO { List getBaseCalendars(); diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/calendars/entities/BaseCalendar.java b/navalplanner-business/src/main/java/org/navalplanner/business/calendars/entities/BaseCalendar.java index d27bb96b4..4ed46d1f9 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/calendars/entities/BaseCalendar.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/calendars/entities/BaseCalendar.java @@ -30,8 +30,9 @@ import java.util.Set; import org.hibernate.validator.NotEmpty; import org.joda.time.DateTimeConstants; import org.joda.time.LocalDate; +import org.navalplanner.business.calendars.daos.IBaseCalendarDAO; import org.navalplanner.business.calendars.entities.CalendarData.Days; -import org.navalplanner.business.common.BaseEntity; +import org.navalplanner.business.common.IntegrationEntity; import org.navalplanner.business.planner.entities.ResourcesPerDay; /** @@ -41,14 +42,12 @@ import org.navalplanner.business.planner.entities.ResourcesPerDay; * some exceptions of its parent calendar. * @author Manuel Rego Casasnovas */ -public class BaseCalendar extends BaseEntity implements IWorkHours { +public class BaseCalendar extends IntegrationEntity implements IWorkHours { private static final Integer DEFAULT_VALUE = 0; public static BaseCalendar create() { - BaseCalendar baseCalendar = new BaseCalendar(CalendarData.create()); - baseCalendar.setNewObject(true); - return baseCalendar; + return create(new BaseCalendar(CalendarData.create())); } @NotEmpty @@ -938,4 +937,9 @@ public class BaseCalendar extends BaseEntity implements IWorkHours { return each.getHours() != 0 || each.getType().isOverAssignable(); } + @Override + protected IBaseCalendarDAO getIntegrationEntityDAO() { + return org.navalplanner.business.common.Registry.getBaseCalendarDAO(); + } + } diff --git a/navalplanner-business/src/main/resources/org/navalplanner/business/calendars/entities/Calendars.hbm.xml b/navalplanner-business/src/main/resources/org/navalplanner/business/calendars/entities/Calendars.hbm.xml index eb030ca70..25e6d97b2 100644 --- a/navalplanner-business/src/main/resources/org/navalplanner/business/calendars/entities/Calendars.hbm.xml +++ b/navalplanner-business/src/main/resources/org/navalplanner/business/calendars/entities/Calendars.hbm.xml @@ -12,6 +12,8 @@ + + diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/calendars/api/BaseCalendarDTO.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/calendars/api/BaseCalendarDTO.java new file mode 100644 index 000000000..dad6cd1ff --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/calendars/api/BaseCalendarDTO.java @@ -0,0 +1,57 @@ +/* + * This file is part of NavalPlan + * + * Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e + * Desenvolvemento Tecnolóxico de Galicia + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package org.navalplanner.ws.calendars.api; + +import javax.xml.bind.annotation.XmlAttribute; + +import org.navalplanner.business.calendars.entities.BaseCalendar; +import org.navalplanner.ws.common.api.IntegrationEntityDTO; + +/** + * DTO for {@link BaseCalendar} entity. + * + * @author Manuel Rego Casasnovas + */ +public class BaseCalendarDTO extends IntegrationEntityDTO { + + public final static String ENTITY_TYPE = "base-calendar"; + + @XmlAttribute + public String name; + + public BaseCalendarDTO() { + } + + public BaseCalendarDTO(String code, String name) { + super(code); + this.name = name; + } + + public BaseCalendarDTO(String name) { + this(generateCode(), name); + } + + @Override + public String getEntityType() { + return ENTITY_TYPE; + } + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/calendars/api/BaseCalendarListDTO.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/calendars/api/BaseCalendarListDTO.java new file mode 100644 index 000000000..ec62aa268 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/calendars/api/BaseCalendarListDTO.java @@ -0,0 +1,48 @@ +/* + * This file is part of NavalPlan + * + * Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e + * Desenvolvemento Tecnolóxico de Galicia + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package org.navalplanner.ws.calendars.api; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +import org.navalplanner.business.calendars.entities.BaseCalendar; + +/** + * DTO for a list of {@link BaseCalendar} entities. + * + * @author Manuel Rego Casasnovas + */ +@XmlRootElement(name = "base-calendar-list") +public class BaseCalendarListDTO { + + @XmlElement(name = "base-calendar") + public List baseCalendars = new ArrayList(); + + public BaseCalendarListDTO() {} + + public BaseCalendarListDTO(List baseCalendars) { + this.baseCalendars = baseCalendars; + } + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/calendars/api/ICalendarService.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/calendars/api/ICalendarService.java new file mode 100644 index 000000000..98b190958 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/calendars/api/ICalendarService.java @@ -0,0 +1,34 @@ +/* + * This file is part of NavalPlan + * + * Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e + * Desenvolvemento Tecnolóxico de Galicia + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package org.navalplanner.ws.calendars.api; + +import org.navalplanner.business.calendars.entities.BaseCalendar; + +/** + * Service for managing {@link BaseCalendar} entities. + * + * @author Manuel Rego Casasnovas + */ +public interface ICalendarService { + + BaseCalendarListDTO getBaseCalendars(); + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/calendars/api/package-info.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/calendars/api/package-info.java new file mode 100644 index 000000000..915b85e43 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/calendars/api/package-info.java @@ -0,0 +1,29 @@ +/* + * This file is part of NavalPlan + * + * Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e + * Desenvolvemento Tecnolóxico de Galicia + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +/** + * Specification of namespace for REST-based services. + * + * @author Manuel Rego Casasnovas + */ +@javax.xml.bind.annotation.XmlSchema(elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED, namespace = WSCommonGlobalNames.REST_NAMESPACE) +package org.navalplanner.ws.calendars.api; + +import org.navalplanner.ws.common.api.WSCommonGlobalNames; diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/calendars/impl/CalendarConverter.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/calendars/impl/CalendarConverter.java new file mode 100644 index 000000000..87b5a307e --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/calendars/impl/CalendarConverter.java @@ -0,0 +1,41 @@ +/* + * This file is part of NavalPlan + * + * Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e + * Desenvolvemento Tecnolóxico de Galicia + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package org.navalplanner.ws.calendars.impl; + +import org.navalplanner.business.calendars.entities.BaseCalendar; +import org.navalplanner.ws.calendars.api.BaseCalendarDTO; + +/** + * Converter from/to {@link BaseCalendar} related entities to/from DTOs. + * + * @author Manuel Rego Casasnovas + */ +public final class CalendarConverter { + + private CalendarConverter() { + } + + public final static BaseCalendarDTO toDTO(BaseCalendar baseCalendar) { + return new BaseCalendarDTO(baseCalendar.getCode(), baseCalendar + .getName()); + } + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/calendars/impl/CalendarServiceREST.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/calendars/impl/CalendarServiceREST.java new file mode 100644 index 000000000..5e43c813f --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/calendars/impl/CalendarServiceREST.java @@ -0,0 +1,87 @@ +/* + * This file is part of NavalPlan + * + * Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e + * Desenvolvemento Tecnolóxico de Galicia + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package org.navalplanner.ws.calendars.impl; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; + +import org.navalplanner.business.calendars.daos.IBaseCalendarDAO; +import org.navalplanner.business.calendars.entities.BaseCalendar; +import org.navalplanner.business.common.daos.IIntegrationEntityDAO; +import org.navalplanner.business.common.exceptions.ValidationException; +import org.navalplanner.ws.calendars.api.BaseCalendarDTO; +import org.navalplanner.ws.calendars.api.BaseCalendarListDTO; +import org.navalplanner.ws.calendars.api.ICalendarService; +import org.navalplanner.ws.common.impl.GenericRESTService; +import org.navalplanner.ws.common.impl.RecoverableErrorException; +import org.navalplanner.ws.labels.api.ILabelService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** + * REST-based implementation of {@link ILabelService}. + * + * @author Manuel Rego Casasnovas + */ +@Path("/calendars/") +@Produces("application/xml") +@Service("calendarServiceREST") +public class CalendarServiceREST extends + GenericRESTService implements + ICalendarService { + + @Autowired + private IBaseCalendarDAO baseCalendarDAO; + + @Override + protected IIntegrationEntityDAO getIntegrationEntityDAO() { + return baseCalendarDAO; + } + + @Override + protected BaseCalendarDTO toDTO(BaseCalendar entity) { + return CalendarConverter.toDTO(entity); + } + + @Override + protected BaseCalendar toEntity(BaseCalendarDTO entityDTO) + throws ValidationException, RecoverableErrorException { + // TODO Auto-generated method stub + return null; + } + + @Override + protected void updateEntity(BaseCalendar entity, BaseCalendarDTO entityDTO) + throws ValidationException, RecoverableErrorException { + // TODO Auto-generated method stub + + } + + @Override + @GET + @Transactional(readOnly = true) + public BaseCalendarListDTO getBaseCalendars() { + return new BaseCalendarListDTO(findAll()); + } + +} \ No newline at end of file diff --git a/navalplanner-webapp/src/main/resources/navalplanner-webapp-spring-config.xml b/navalplanner-webapp/src/main/resources/navalplanner-webapp-spring-config.xml index ad9656f0d..5ade06edc 100644 --- a/navalplanner-webapp/src/main/resources/navalplanner-webapp-spring-config.xml +++ b/navalplanner-webapp/src/main/resources/navalplanner-webapp-spring-config.xml @@ -72,6 +72,7 @@ + diff --git a/navalplanner-webapp/src/test/java/org/navalplanner/web/test/ws/labels/api/CalendarServiceTest.java b/navalplanner-webapp/src/test/java/org/navalplanner/web/test/ws/labels/api/CalendarServiceTest.java new file mode 100644 index 000000000..54014e279 --- /dev/null +++ b/navalplanner-webapp/src/test/java/org/navalplanner/web/test/ws/labels/api/CalendarServiceTest.java @@ -0,0 +1,101 @@ +/* + * This file is part of NavalPlan + * + * Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e + * Desenvolvemento Tecnolóxico de Galicia + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package org.navalplanner.web.test.ws.labels.api; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; +import static org.navalplanner.business.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_FILE; +import static org.navalplanner.web.WebappGlobalNames.WEBAPP_SPRING_CONFIG_FILE; +import static org.navalplanner.web.test.WebappGlobalNames.WEBAPP_SPRING_CONFIG_TEST_FILE; + +import org.hibernate.SessionFactory; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.navalplanner.business.calendars.daos.IBaseCalendarDAO; +import org.navalplanner.business.calendars.entities.BaseCalendar; +import org.navalplanner.business.common.IAdHocTransactionService; +import org.navalplanner.ws.calendars.api.BaseCalendarDTO; +import org.navalplanner.ws.calendars.api.BaseCalendarListDTO; +import org.navalplanner.ws.calendars.api.ICalendarService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.transaction.annotation.Transactional; + +/** + * Tests for {@link ICalendarService}. + * + * @author Manuel Rego Casasnovas + */ + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = { BUSINESS_SPRING_CONFIG_FILE, + WEBAPP_SPRING_CONFIG_FILE, WEBAPP_SPRING_CONFIG_TEST_FILE }) +@Transactional +public class CalendarServiceTest { + + @Autowired + private ICalendarService calendarService; + + @Autowired + private IBaseCalendarDAO baseCalendarDAO; + + @Autowired + private SessionFactory sessionFactory; + + @Autowired + private IAdHocTransactionService transactionService; + + private BaseCalendar givenBaseCalendarStored() { + BaseCalendar calendar = BaseCalendar.create(); + calendar.setName("calendar-name"); + + baseCalendarDAO.save(calendar); + baseCalendarDAO.flush(); + sessionFactory.getCurrentSession().evict(calendar); + calendar.dontPoseAsTransientObjectAnymore(); + + return calendar; + } + + @Test + public void exportBaseCalendars() { + int previous = baseCalendarDAO.getBaseCalendars().size(); + + BaseCalendarListDTO baseCalendars = calendarService.getBaseCalendars(); + assertThat(baseCalendars.baseCalendars.size(), equalTo(previous)); + } + + @Test + public void exportBaseCalendars2() { + int previous = baseCalendarDAO.getBaseCalendars().size(); + + BaseCalendar calendar = givenBaseCalendarStored(); + + BaseCalendarListDTO baseCalendars = calendarService.getBaseCalendars(); + assertThat(baseCalendars.baseCalendars.size(), equalTo(previous + 1)); + + BaseCalendarDTO calendarDTO = baseCalendars.baseCalendars.get(previous); + assertThat(calendarDTO.code, equalTo(calendar.getCode())); + assertThat(calendarDTO.name, equalTo(calendar.getName())); + } + +}