diff --git a/libreplan-business/src/main/java/org/libreplan/business/workreports/daos/IWorkReportDAO.java b/libreplan-business/src/main/java/org/libreplan/business/workreports/daos/IWorkReportDAO.java index 657d7eba1..5201a5805 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/workreports/daos/IWorkReportDAO.java +++ b/libreplan-business/src/main/java/org/libreplan/business/workreports/daos/IWorkReportDAO.java @@ -59,4 +59,7 @@ public interface IWorkReportDAO extends IIntegrationEntityDAO { boolean isAnyPersonalTimesheetAlreadySaved(); + List findPersonalTimesheetsByResourceAndOrderElement( + Resource resource); + } diff --git a/libreplan-business/src/main/java/org/libreplan/business/workreports/daos/IWorkReportLineDAO.java b/libreplan-business/src/main/java/org/libreplan/business/workreports/daos/IWorkReportLineDAO.java index 61b410c95..8b37b1874 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/workreports/daos/IWorkReportLineDAO.java +++ b/libreplan-business/src/main/java/org/libreplan/business/workreports/daos/IWorkReportLineDAO.java @@ -65,4 +65,7 @@ public interface IWorkReportLineDAO extends List findByResourceFilteredByDateNotInWorkReport( Resource resource, Date start, Date end, WorkReport workReport); + List findByOrderElementAndWorkReports( + OrderElement orderElement, List workReports); + } diff --git a/libreplan-business/src/main/java/org/libreplan/business/workreports/daos/WorkReportDAO.java b/libreplan-business/src/main/java/org/libreplan/business/workreports/daos/WorkReportDAO.java index dfd62ad74..3c16fef78 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/workreports/daos/WorkReportDAO.java +++ b/libreplan-business/src/main/java/org/libreplan/business/workreports/daos/WorkReportDAO.java @@ -143,19 +143,9 @@ public class WorkReportDAO extends IntegrationEntityDAO @SuppressWarnings("unchecked") public WorkReport getPersonalTimesheetWorkReport(Resource resource, LocalDate date, PersonalTimesheetsPeriodicityEnum periodicity) { - WorkReportType workReportType; - try { - workReportType = workReportTypeDAO - .findUniqueByName(PredefinedWorkReportTypes.PERSONAL_TIMESHEETS - .getName()); - } catch (NonUniqueResultException e) { - throw new RuntimeException(e); - } catch (InstanceNotFoundException e) { - throw new RuntimeException(e); - } - Criteria criteria = getSession().createCriteria(WorkReport.class); - criteria.add(Restrictions.eq("workReportType", workReportType)); + criteria.add(Restrictions.eq("workReportType", + getPersonalTimesheetsWorkReportType())); List personalTimesheets = criteria.add( Restrictions.eq("resource", resource)).list(); @@ -179,8 +169,7 @@ public class WorkReportDAO extends IntegrationEntityDAO return null; } - @Override - public boolean isAnyPersonalTimesheetAlreadySaved() { + private WorkReportType getPersonalTimesheetsWorkReportType() { WorkReportType workReportType; try { workReportType = workReportTypeDAO @@ -191,10 +180,29 @@ public class WorkReportDAO extends IntegrationEntityDAO } catch (InstanceNotFoundException e) { throw new RuntimeException(e); } + return workReportType; + } + + @Override + public boolean isAnyPersonalTimesheetAlreadySaved() { + WorkReportType workReportType = getPersonalTimesheetsWorkReportType(); Criteria criteria = getSession().createCriteria(WorkReport.class); criteria.add(Restrictions.eq("workReportType", workReportType)); return criteria.list().isEmpty(); } + @Override + @SuppressWarnings("unchecked") + public List findPersonalTimesheetsByResourceAndOrderElement( + Resource resource) { + Criteria criteria = getSession().createCriteria(WorkReport.class); + + criteria.add(Restrictions.eq("workReportType", + getPersonalTimesheetsWorkReportType())); + criteria.add(Restrictions.eq("resource", resource)); + + return criteria.list(); + } + } diff --git a/libreplan-business/src/main/java/org/libreplan/business/workreports/daos/WorkReportLineDAO.java b/libreplan-business/src/main/java/org/libreplan/business/workreports/daos/WorkReportLineDAO.java index 4a40878d5..be6162603 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/workreports/daos/WorkReportLineDAO.java +++ b/libreplan-business/src/main/java/org/libreplan/business/workreports/daos/WorkReportLineDAO.java @@ -146,4 +146,16 @@ public class WorkReportLineDAO extends IntegrationEntityDAO return criteria.list(); } + @SuppressWarnings("unchecked") + @Override + public List findByOrderElementAndWorkReports( + OrderElement orderElement, List workReports) { + Criteria criteria = getSession().createCriteria(WorkReportLine.class); + + criteria.add(Restrictions.eq("orderElement", orderElement)); + criteria.add(Restrictions.in("workReport", workReports)); + + return (List) criteria.list(); + } + } diff --git a/libreplan-webapp/src/main/java/org/libreplan/web/users/dashboard/UserDashboardUtil.java b/libreplan-webapp/src/main/java/org/libreplan/web/users/dashboard/UserDashboardUtil.java index 72972d7d0..0d1829bf6 100644 --- a/libreplan-webapp/src/main/java/org/libreplan/web/users/dashboard/UserDashboardUtil.java +++ b/libreplan-webapp/src/main/java/org/libreplan/web/users/dashboard/UserDashboardUtil.java @@ -24,6 +24,7 @@ import java.util.List; import org.libreplan.business.resources.entities.Resource; import org.libreplan.business.users.entities.User; +import org.libreplan.web.UserUtil; /** * Utilities class for user dashboard window @@ -38,4 +39,12 @@ public class UserDashboardUtil { return resource; } + public static Resource getBoundResourceFromSession() { + User user = UserUtil.getUserFromSession(); + if (user.isBound()) { + return user.getWorker(); + } + return null; + } + } diff --git a/libreplan-webapp/src/main/java/org/libreplan/ws/boundusers/api/IBoundUserService.java b/libreplan-webapp/src/main/java/org/libreplan/ws/boundusers/api/IBoundUserService.java index 666879af2..399d50278 100644 --- a/libreplan-webapp/src/main/java/org/libreplan/ws/boundusers/api/IBoundUserService.java +++ b/libreplan-webapp/src/main/java/org/libreplan/ws/boundusers/api/IBoundUserService.java @@ -21,6 +21,8 @@ package org.libreplan.ws.boundusers.api; +import javax.ws.rs.core.Response; + /** * Service for managing operations related with bound users. * @@ -30,4 +32,6 @@ public interface IBoundUserService { TaskListDTO getTasks(); + Response getTimesheetEntriesByTask(String taskCode); + } diff --git a/libreplan-webapp/src/main/java/org/libreplan/ws/boundusers/api/PersonalTimesheetEntryDTO.java b/libreplan-webapp/src/main/java/org/libreplan/ws/boundusers/api/PersonalTimesheetEntryDTO.java new file mode 100644 index 000000000..232b13172 --- /dev/null +++ b/libreplan-webapp/src/main/java/org/libreplan/ws/boundusers/api/PersonalTimesheetEntryDTO.java @@ -0,0 +1,52 @@ +/* + * This file is part of LibrePlan + * + * Copyright (C) 2012 Igalia, S.L. + * + * 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.libreplan.ws.boundusers.api; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.datatype.XMLGregorianCalendar; + +/** + * DTO for an entry in a personal timesheet. + * + * @author Manuel Rego Casasnovas + */ +@XmlRootElement(name = "personal-timesheet-entry") +public class PersonalTimesheetEntryDTO { + + @XmlAttribute + public String task; + + @XmlAttribute(name = "date") + public XMLGregorianCalendar date; + + @XmlAttribute + public String effort; + + public PersonalTimesheetEntryDTO() {} + + public PersonalTimesheetEntryDTO(String task, XMLGregorianCalendar date, + String effort) { + this.task = task; + this.date = date; + this.effort = effort; + } + +} diff --git a/libreplan-webapp/src/main/java/org/libreplan/ws/boundusers/api/PersonalTimesheetEntryListDTO.java b/libreplan-webapp/src/main/java/org/libreplan/ws/boundusers/api/PersonalTimesheetEntryListDTO.java new file mode 100644 index 000000000..73b2dc007 --- /dev/null +++ b/libreplan-webapp/src/main/java/org/libreplan/ws/boundusers/api/PersonalTimesheetEntryListDTO.java @@ -0,0 +1,45 @@ +/* + * This file is part of LibrePlan + * + * Copyright (C) 2012 Igalia, S.L. + * + * 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.libreplan.ws.boundusers.api; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * DTO for a list of personal timesheet entries. + * + * @author Manuel Rego Casasnovas + */ +@XmlRootElement(name = "personal-timesheet-entry-list") +public class PersonalTimesheetEntryListDTO { + + @XmlElement(name = "personal-timesheet-entry") + public List entries = new ArrayList(); + + public PersonalTimesheetEntryListDTO() {} + + public PersonalTimesheetEntryListDTO(List entries) { + this.entries = entries; + } + +} diff --git a/libreplan-webapp/src/main/java/org/libreplan/ws/boundusers/impl/BoundUserServiceREST.java b/libreplan-webapp/src/main/java/org/libreplan/ws/boundusers/impl/BoundUserServiceREST.java index 3249460e8..5b6c9b2a2 100644 --- a/libreplan-webapp/src/main/java/org/libreplan/ws/boundusers/impl/BoundUserServiceREST.java +++ b/libreplan-webapp/src/main/java/org/libreplan/ws/boundusers/impl/BoundUserServiceREST.java @@ -19,15 +19,30 @@ package org.libreplan.ws.boundusers.impl; +import java.util.List; + import javax.ws.rs.GET; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; +import org.libreplan.business.common.exceptions.InstanceNotFoundException; +import org.libreplan.business.orders.daos.IOrderElementDAO; +import org.libreplan.business.orders.entities.OrderElement; +import org.libreplan.business.workreports.daos.IWorkReportDAO; +import org.libreplan.business.workreports.daos.IWorkReportLineDAO; +import org.libreplan.business.workreports.entities.WorkReport; +import org.libreplan.business.workreports.entities.WorkReportLine; import org.libreplan.web.users.dashboard.IMyTasksAreaModel; +import org.libreplan.web.users.dashboard.UserDashboardUtil; import org.libreplan.ws.boundusers.api.IBoundUserService; +import org.libreplan.ws.boundusers.api.PersonalTimesheetEntryListDTO; import org.libreplan.ws.boundusers.api.TaskListDTO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; /** * REST-based implementation of {@link IBoundUserService}; @@ -42,11 +57,43 @@ public class BoundUserServiceREST implements IBoundUserService { @Autowired private IMyTasksAreaModel myTasksAreaModel; + @Autowired + private IOrderElementDAO orderElementDAO; + + @Autowired + private IWorkReportDAO workReportDAO; + + @Autowired + private IWorkReportLineDAO workReportLineDAO; + @Override @GET + @Transactional(readOnly = true) @Path("/mytasks/") public TaskListDTO getTasks() { return TaskConverter.toDTO(myTasksAreaModel.getTasks()); } + @Override + @GET + @Transactional(readOnly = true) + @Path("/timesheets/{task-code}/") + public Response getTimesheetEntriesByTask( + @PathParam("task-code") String taskCode) { + try { + OrderElement orderElement = orderElementDAO.findByCode(taskCode); + List workReports = workReportDAO + .findPersonalTimesheetsByResourceAndOrderElement(UserDashboardUtil + .getBoundResourceFromSession()); + List workReportLines = workReportLineDAO + .findByOrderElementAndWorkReports(orderElement, workReports); + + PersonalTimesheetEntryListDTO dto = PersonalTimesheetEntryConverter + .toDTO(workReportLines); + return Response.ok(dto).build(); + } catch (InstanceNotFoundException e) { + return Response.status(Status.NOT_FOUND).build(); + } + } + } diff --git a/libreplan-webapp/src/main/java/org/libreplan/ws/boundusers/impl/PersonalTimesheetEntryConverter.java b/libreplan-webapp/src/main/java/org/libreplan/ws/boundusers/impl/PersonalTimesheetEntryConverter.java new file mode 100644 index 000000000..f5874d7fc --- /dev/null +++ b/libreplan-webapp/src/main/java/org/libreplan/ws/boundusers/impl/PersonalTimesheetEntryConverter.java @@ -0,0 +1,57 @@ +/* + * This file is part of LibrePlan + * + * Copyright (C) 2012 Igalia, S.L. + * + * 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.libreplan.ws.boundusers.impl; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.libreplan.business.workreports.entities.WorkReportLine; +import org.libreplan.ws.boundusers.api.PersonalTimesheetEntryDTO; +import org.libreplan.ws.boundusers.api.PersonalTimesheetEntryListDTO; +import org.libreplan.ws.common.impl.DateConverter; + +/** + * Converter from/to {@link WorkReportLine} to/from DTOs. + * + * @author Manuel Rego Casasnovas + */ +public final class PersonalTimesheetEntryConverter { + + private PersonalTimesheetEntryConverter() { + } + + public final static PersonalTimesheetEntryDTO toDTO( + WorkReportLine workReportLine) { + return new PersonalTimesheetEntryDTO(workReportLine.getOrderElement() + .getCode(), DateConverter.toXMLGregorianCalendar(workReportLine + .getDate()), workReportLine.getEffort().toFormattedString()); + } + + public final static PersonalTimesheetEntryListDTO toDTO( + Collection workReportLines) { + List dtos = new ArrayList(); + for (WorkReportLine line : workReportLines) { + dtos.add(toDTO(line)); + } + return new PersonalTimesheetEntryListDTO(dtos); + } + +}