New web service returning the personal timesheets data for a task of a bound user

FEA: ItEr77S14BoundUsersWebServices
This commit is contained in:
Manuel Rego Casasnovas 2012-11-07 22:34:51 +01:00
parent cc87e7323a
commit 4b2b1461d3
10 changed files with 254 additions and 14 deletions

View file

@ -59,4 +59,7 @@ public interface IWorkReportDAO extends IIntegrationEntityDAO<WorkReport> {
boolean isAnyPersonalTimesheetAlreadySaved();
List<WorkReport> findPersonalTimesheetsByResourceAndOrderElement(
Resource resource);
}

View file

@ -65,4 +65,7 @@ public interface IWorkReportLineDAO extends
List<WorkReportLine> findByResourceFilteredByDateNotInWorkReport(
Resource resource, Date start, Date end, WorkReport workReport);
List<WorkReportLine> findByOrderElementAndWorkReports(
OrderElement orderElement, List<WorkReport> workReports);
}

View file

@ -143,19 +143,9 @@ public class WorkReportDAO extends IntegrationEntityDAO<WorkReport>
@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<WorkReport> personalTimesheets = criteria.add(
Restrictions.eq("resource", resource)).list();
@ -179,8 +169,7 @@ public class WorkReportDAO extends IntegrationEntityDAO<WorkReport>
return null;
}
@Override
public boolean isAnyPersonalTimesheetAlreadySaved() {
private WorkReportType getPersonalTimesheetsWorkReportType() {
WorkReportType workReportType;
try {
workReportType = workReportTypeDAO
@ -191,10 +180,29 @@ public class WorkReportDAO extends IntegrationEntityDAO<WorkReport>
} 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<WorkReport> findPersonalTimesheetsByResourceAndOrderElement(
Resource resource) {
Criteria criteria = getSession().createCriteria(WorkReport.class);
criteria.add(Restrictions.eq("workReportType",
getPersonalTimesheetsWorkReportType()));
criteria.add(Restrictions.eq("resource", resource));
return criteria.list();
}
}

View file

@ -146,4 +146,16 @@ public class WorkReportLineDAO extends IntegrationEntityDAO<WorkReportLine>
return criteria.list();
}
@SuppressWarnings("unchecked")
@Override
public List<WorkReportLine> findByOrderElementAndWorkReports(
OrderElement orderElement, List<WorkReport> workReports) {
Criteria criteria = getSession().createCriteria(WorkReportLine.class);
criteria.add(Restrictions.eq("orderElement", orderElement));
criteria.add(Restrictions.in("workReport", workReports));
return (List<WorkReportLine>) criteria.list();
}
}

View file

@ -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;
}
}

View file

@ -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);
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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 <rego@igalia.com>
*/
@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;
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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 <rego@igalia.com>
*/
@XmlRootElement(name = "personal-timesheet-entry-list")
public class PersonalTimesheetEntryListDTO {
@XmlElement(name = "personal-timesheet-entry")
public List<PersonalTimesheetEntryDTO> entries = new ArrayList<PersonalTimesheetEntryDTO>();
public PersonalTimesheetEntryListDTO() {}
public PersonalTimesheetEntryListDTO(List<PersonalTimesheetEntryDTO> entries) {
this.entries = entries;
}
}

View file

@ -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<WorkReport> workReports = workReportDAO
.findPersonalTimesheetsByResourceAndOrderElement(UserDashboardUtil
.getBoundResourceFromSession());
List<WorkReportLine> 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();
}
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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 <rego@igalia.com>
*/
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<WorkReportLine> workReportLines) {
List<PersonalTimesheetEntryDTO> dtos = new ArrayList<PersonalTimesheetEntryDTO>();
for (WorkReportLine line : workReportLines) {
dtos.add(toDTO(line));
}
return new PersonalTimesheetEntryListDTO(dtos);
}
}