ItEr51S10AdaptacionServiciosRESTItEr50S13: Added export resources service.

This commit is contained in:
Manuel Rego Casasnovas 2010-03-18 16:39:26 +01:00 committed by Javier Moran Rua
parent daabf1ce4c
commit bafae1638a
8 changed files with 167 additions and 161 deletions

View file

@ -1,75 +0,0 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package org.navalplanner.ws.common.impl;
import java.util.Date;
import javax.xml.datatype.XMLGregorianCalendar;
import org.joda.time.LocalDate;
/**
* A converter from <code>java.util.Date</code> to/from
* <code>javax.xml.datatype.XMLGregorianCalendar</code>.
*
* @author Fernando Bellas Permuy <fbellas@udc.es>
*/
public class DateConverter {
private DateConverter() {}
/**
* It converts a <code>XMLGregorianCalendar</code> representing a
* <code>xsd:date</code> XML type to a <code>Date</code>.<br/><br/>
*
* If the date passed as a parameter is <code>null</code>, it also returns
* <code>null</code>.
*/
public final static Date toDate(XMLGregorianCalendar date) {
if (date == null) {
return null;
} else {
return date.toGregorianCalendar().getTime();
}
}
/**
* It converts a <code>XMLGregorianCalendar</code> representing a
* <code>xsd:date</code> XML type to a Joda's <code>LocalDate</code>.
* <br/><br/>
*
* If the date passed as a parameter is <code>null</code>, it also returns
* <code>null</code>.
*/
public final static LocalDate toLocalDate(XMLGregorianCalendar date) {
if (date == null) {
return null;
} else {
return new LocalDate(date.getYear(), date.getMonth(),
date.getDay());
}
}
}

View file

@ -20,9 +20,9 @@
package org.navalplanner.ws.resources.api; package org.navalplanner.ws.resources.api;
import java.util.Date;
import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.datatype.XMLGregorianCalendar;
import org.navalplanner.ws.common.api.IntegrationEntityDTO; import org.navalplanner.ws.common.api.IntegrationEntityDTO;
@ -42,18 +42,16 @@ public class CriterionSatisfactionDTO extends IntegrationEntityDTO {
public String criterionName; public String criterionName;
@XmlAttribute(name="start-date") @XmlAttribute(name="start-date")
@XmlSchemaType(name="date") public Date startDate;
public XMLGregorianCalendar startDate;
@XmlAttribute(name="end-date") @XmlAttribute(name="end-date")
@XmlSchemaType(name="date") public Date endDate;
public XMLGregorianCalendar endDate;
public CriterionSatisfactionDTO() {} public CriterionSatisfactionDTO() {}
public CriterionSatisfactionDTO(String code, public CriterionSatisfactionDTO(String code,
String criterionTypeName, String criterionName, String criterionTypeName, String criterionName,
XMLGregorianCalendar startDate, XMLGregorianCalendar endDate) { Date startDate, Date endDate) {
super(code); super(code);
this.criterionTypeName = criterionTypeName; this.criterionTypeName = criterionTypeName;
@ -70,7 +68,7 @@ public class CriterionSatisfactionDTO extends IntegrationEntityDTO {
*/ */
public CriterionSatisfactionDTO( public CriterionSatisfactionDTO(
String criterionTypeName, String criterionName, String criterionTypeName, String criterionName,
XMLGregorianCalendar startDate, XMLGregorianCalendar endDate) { Date startDate, Date endDate) {
this(generateCode(), criterionTypeName, criterionName, startDate, this(generateCode(), criterionTypeName, criterionName, startDate,
endDate); endDate);

View file

@ -35,6 +35,8 @@ import org.navalplanner.ws.common.api.InstanceConstraintViolationsListDTO;
*/ */
public interface IResourceService { public interface IResourceService {
public ResourceListDTO getResources();
public InstanceConstraintViolationsListDTO addResources( public InstanceConstraintViolationsListDTO addResources(
ResourceListDTO resources); ResourceListDTO resources);

View file

@ -19,9 +19,9 @@
*/ */
package org.navalplanner.ws.resources.api; package org.navalplanner.ws.resources.api;
import java.util.Date;
import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.datatype.XMLGregorianCalendar;
import org.navalplanner.ws.common.api.IntegrationEntityDTO; import org.navalplanner.ws.common.api.IntegrationEntityDTO;
@ -39,18 +39,15 @@ public class ResourcesCostCategoryAssignmentDTO extends IntegrationEntityDTO {
public String costCategoryName; public String costCategoryName;
@XmlAttribute(name="start-date") @XmlAttribute(name="start-date")
@XmlSchemaType(name="date") public Date startDate;
public XMLGregorianCalendar startDate;
@XmlAttribute(name="end-date") @XmlAttribute(name="end-date")
@XmlSchemaType(name="date") public Date endDate;
public XMLGregorianCalendar endDate;
public ResourcesCostCategoryAssignmentDTO() {} public ResourcesCostCategoryAssignmentDTO() {}
public ResourcesCostCategoryAssignmentDTO(String code, public ResourcesCostCategoryAssignmentDTO(String code,
String costCategoryName, XMLGregorianCalendar startDate, String costCategoryName, Date startDate, Date endDate) {
XMLGregorianCalendar endDate) {
super(code); super(code);
this.costCategoryName = costCategoryName; this.costCategoryName = costCategoryName;
@ -64,9 +61,8 @@ public class ResourcesCostCategoryAssignmentDTO extends IntegrationEntityDTO {
* to facilitate the implementation of test cases that add new instances * to facilitate the implementation of test cases that add new instances
* (such instances will have a unique code). * (such instances will have a unique code).
*/ */
public ResourcesCostCategoryAssignmentDTO( public ResourcesCostCategoryAssignmentDTO(String costCategoryName,
String costCategoryName, XMLGregorianCalendar startDate, Date startDate, Date endDate) {
XMLGregorianCalendar endDate) {
this(generateCode(), costCategoryName, startDate, endDate); this(generateCode(), costCategoryName, startDate, endDate);

View file

@ -22,9 +22,12 @@ package org.navalplanner.ws.resources.impl;
import static org.navalplanner.web.I18nHelper._; import static org.navalplanner.web.I18nHelper._;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.joda.time.LocalDate;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException; import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.common.exceptions.MultipleInstancesException; import org.navalplanner.business.common.exceptions.MultipleInstancesException;
import org.navalplanner.business.common.exceptions.ValidationException; import org.navalplanner.business.common.exceptions.ValidationException;
@ -34,7 +37,6 @@ import org.navalplanner.business.resources.entities.CriterionType;
import org.navalplanner.business.resources.entities.Machine; import org.navalplanner.business.resources.entities.Machine;
import org.navalplanner.business.resources.entities.Resource; import org.navalplanner.business.resources.entities.Resource;
import org.navalplanner.business.resources.entities.Worker; import org.navalplanner.business.resources.entities.Worker;
import org.navalplanner.ws.common.impl.DateConverter;
import org.navalplanner.ws.common.impl.InstanceNotFoundRecoverableErrorException; import org.navalplanner.ws.common.impl.InstanceNotFoundRecoverableErrorException;
import org.navalplanner.ws.common.impl.RecoverableErrorException; import org.navalplanner.ws.common.impl.RecoverableErrorException;
import org.navalplanner.ws.resources.api.CriterionSatisfactionDTO; import org.navalplanner.ws.resources.api.CriterionSatisfactionDTO;
@ -158,9 +160,8 @@ public class ResourceConverter {
StringUtils.trim(criterionSatisfactionDTO.code), StringUtils.trim(criterionSatisfactionDTO.code),
StringUtils.trim(criterionSatisfactionDTO.criterionTypeName), StringUtils.trim(criterionSatisfactionDTO.criterionTypeName),
StringUtils.trim(criterionSatisfactionDTO.criterionName), StringUtils.trim(criterionSatisfactionDTO.criterionName),
resource, resource, criterionSatisfactionDTO.startDate,
DateConverter.toDate(criterionSatisfactionDTO.startDate), criterionSatisfactionDTO.endDate);
DateConverter.toDate(criterionSatisfactionDTO.endDate));
} catch (InstanceNotFoundException e) { } catch (InstanceNotFoundException e) {
@ -216,11 +217,14 @@ public class ResourceConverter {
} }
try { try {
LocalDate startDate = (assignmentDTO.startDate == null) ? null
: LocalDate.fromDateFields(assignmentDTO.startDate);
LocalDate endDate = (assignmentDTO.endDate == null) ? null
: LocalDate.fromDateFields(assignmentDTO.endDate);
return ResourcesCostCategoryAssignment.createUnvalidated( return ResourcesCostCategoryAssignment.createUnvalidated(
assignmentDTO.code, assignmentDTO.code, StringUtils
StringUtils.trim(assignmentDTO.costCategoryName), resource, .trim(assignmentDTO.costCategoryName), resource,
DateConverter.toLocalDate(assignmentDTO.startDate), startDate, endDate);
DateConverter.toLocalDate(assignmentDTO.endDate));
} catch (InstanceNotFoundException e) { } catch (InstanceNotFoundException e) {
throw new InstanceNotFoundRecoverableErrorException( throw new InstanceNotFoundRecoverableErrorException(
COST_CATEGORY_ENTITY_TYPE, e.getKey().toString()); COST_CATEGORY_ENTITY_TYPE, e.getKey().toString());
@ -302,8 +306,8 @@ public class ResourceConverter {
criterionSatisfaction.updateUnvalidated( criterionSatisfaction.updateUnvalidated(
StringUtils.trim(criterionSatisfactionDTO.criterionTypeName), StringUtils.trim(criterionSatisfactionDTO.criterionTypeName),
StringUtils.trim(criterionSatisfactionDTO.criterionName), StringUtils.trim(criterionSatisfactionDTO.criterionName),
DateConverter.toDate(criterionSatisfactionDTO.startDate), criterionSatisfactionDTO.startDate,
DateConverter.toDate(criterionSatisfactionDTO.endDate)); criterionSatisfactionDTO.endDate);
} catch (InstanceNotFoundException e) { } catch (InstanceNotFoundException e) {
@ -351,10 +355,13 @@ public class ResourceConverter {
ResourcesCostCategoryAssignmentDTO i) { ResourcesCostCategoryAssignmentDTO i) {
try { try {
LocalDate startDate = (i.startDate == null) ? null : LocalDate
.fromDateFields(i.startDate);
LocalDate endDate = (i.endDate == null) ? null : LocalDate
.fromDateFields(i.endDate);
assignment.updateUnvalidated( assignment.updateUnvalidated(
StringUtils.trim(i.costCategoryName), StringUtils.trim(i.costCategoryName),
DateConverter.toLocalDate(i.startDate), startDate, endDate);
DateConverter.toLocalDate(i.endDate));
} catch (InstanceNotFoundException e) { } catch (InstanceNotFoundException e) {
throw new InstanceNotFoundRecoverableErrorException( throw new InstanceNotFoundRecoverableErrorException(
COST_CATEGORY_ENTITY_TYPE, e.getKey().toString()); COST_CATEGORY_ENTITY_TYPE, e.getKey().toString());
@ -375,4 +382,66 @@ public class ResourceConverter {
} }
} public static ResourceDTO toDTO(Resource resource) {
ResourceDTO resourceDTO;
if (resource instanceof Worker) {
resourceDTO = toDTO((Worker) resource);
} else if (resource instanceof Machine) {
resourceDTO = toDTO((Machine) resource);
} else {
return null;
}
List<CriterionSatisfactionDTO> criterionSatisfactionDTOs = new ArrayList<CriterionSatisfactionDTO>();
for (CriterionSatisfaction criterionSatisfaction : resource
.getCriterionSatisfactions()) {
criterionSatisfactionDTOs.add(toDTO(criterionSatisfaction));
}
resourceDTO.criterionSatisfactions = criterionSatisfactionDTOs;
List<ResourcesCostCategoryAssignmentDTO> resourcesCostCategoryAssignmentDTOs = new ArrayList<ResourcesCostCategoryAssignmentDTO>();
for (ResourcesCostCategoryAssignment resourcesCostCategoryAssignment : resource
.getResourcesCostCategoryAssignments()) {
resourcesCostCategoryAssignmentDTOs
.add(toDTO(resourcesCostCategoryAssignment));
}
resourceDTO.resourcesCostCategoryAssignments = resourcesCostCategoryAssignmentDTOs;
return resourceDTO;
}
private static WorkerDTO toDTO(Worker worker) {
return new WorkerDTO(worker.getCode(), worker.getFirstName(), worker
.getSurname(), worker.getNif());
}
private static MachineDTO toDTO(Machine machine) {
return new MachineDTO(machine.getCode(), machine.getName(), machine
.getDescription());
}
private static CriterionSatisfactionDTO toDTO(
CriterionSatisfaction criterionSatisfaction) {
return new CriterionSatisfactionDTO(criterionSatisfaction.getCode(),
criterionSatisfaction.getCriterion().getType().getName(),
criterionSatisfaction.getCriterion().getName(),
criterionSatisfaction.getStartDate(), criterionSatisfaction
.getEndDate());
}
private static ResourcesCostCategoryAssignmentDTO toDTO(
ResourcesCostCategoryAssignment resourcesCostCategoryAssignment) {
Date initDate = (resourcesCostCategoryAssignment.getInitDate() == null) ? null
: resourcesCostCategoryAssignment.getInitDate()
.toDateTimeAtStartOfDay().toDate();
Date endDate = (resourcesCostCategoryAssignment.getEndDate() == null) ? null
: resourcesCostCategoryAssignment.getEndDate()
.toDateTimeAtStartOfDay().toDate();
return new ResourcesCostCategoryAssignmentDTO(
resourcesCostCategoryAssignment.getCode(),
resourcesCostCategoryAssignment.getCostCategory().getName(),
initDate, endDate);
}
}

View file

@ -20,14 +20,20 @@
package org.navalplanner.ws.resources.impl; package org.navalplanner.ws.resources.impl;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import org.navalplanner.business.common.daos.IIntegrationEntityDAO; import org.navalplanner.business.common.daos.IIntegrationEntityDAO;
import org.navalplanner.business.common.exceptions.ValidationException; import org.navalplanner.business.common.exceptions.ValidationException;
import org.navalplanner.business.resources.daos.IMachineDAO;
import org.navalplanner.business.resources.daos.IResourceDAO; import org.navalplanner.business.resources.daos.IResourceDAO;
import org.navalplanner.business.resources.daos.IWorkerDAO;
import org.navalplanner.business.resources.entities.Resource; import org.navalplanner.business.resources.entities.Resource;
import org.navalplanner.ws.common.api.InstanceConstraintViolationsListDTO; import org.navalplanner.ws.common.api.InstanceConstraintViolationsListDTO;
import org.navalplanner.ws.common.impl.GenericRESTService; import org.navalplanner.ws.common.impl.GenericRESTService;
@ -37,6 +43,7 @@ import org.navalplanner.ws.resources.api.ResourceDTO;
import org.navalplanner.ws.resources.api.ResourceListDTO; import org.navalplanner.ws.resources.api.ResourceListDTO;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/** /**
* REST-based implementation of <code>IResourceService</code>. * REST-based implementation of <code>IResourceService</code>.
@ -53,6 +60,27 @@ public class ResourceServiceREST
@Autowired @Autowired
private IResourceDAO resourceDAO; private IResourceDAO resourceDAO;
@Autowired
private IWorkerDAO workerDAO;
@Autowired
private IMachineDAO machineDAO;
@Override
@GET
@Transactional(readOnly = true)
public ResourceListDTO getResources() {
return new ResourceListDTO(findAll());
}
@Override
protected List<ResourceDTO> findAll() {
List<Resource> result = new ArrayList<Resource>();
result.addAll(workerDAO.getWorkers());
result.addAll(machineDAO.getAll());
return toDTO(result);
}
@Override @Override
@POST @POST
@Consumes("application/xml") @Consumes("application/xml")
@ -73,7 +101,7 @@ public class ResourceServiceREST
@Override @Override
protected ResourceDTO toDTO(Resource entity) { protected ResourceDTO toDTO(Resource entity) {
return null; // This service does not provide finder methods. return ResourceConverter.toDTO(entity);
} }
@Override @Override

View file

@ -39,11 +39,6 @@ import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.List; import java.util.List;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -798,12 +793,10 @@ public class ResourceServiceTest {
assertEquals(m1Updated.name, m1Entity.getName()); // Modified. assertEquals(m1Updated.name, m1Entity.getName()); // Modified.
assertEquals(m1.description, m1Entity.getDescription()); //Not modified. assertEquals(m1.description, m1Entity.getDescription()); //Not modified.
assertTrue(datesEquals( // Modified. assertTrue(m1s1Updated.startDate.equals(m1Entity
m1s1Updated.startDate, .getCriterionSatisfactionByCode(m1s1.code).getStartDate()));
m1Entity.getCriterionSatisfactionByCode(m1s1.code).getStartDate())); assertTrue(m1s1.endDate.equals(m1Entity.getCriterionSatisfactionByCode(
assertTrue(datesEquals( // Not modified. m1s1.code).getEndDate())); // Not modified.
m1s1.endDate,
m1Entity.getCriterionSatisfactionByCode(m1s1.code).getEndDate()));
m1Entity.getResourcesCostCategoryAssignmentByCode(m1a2.code); // New. m1Entity.getResourcesCostCategoryAssignmentByCode(m1a2.code); // New.
/* Test worker update. */ /* Test worker update. */
@ -812,14 +805,14 @@ public class ResourceServiceTest {
assertEquals(w1Updated.surname, w1Entity.getSurname()); // Modified. assertEquals(w1Updated.surname, w1Entity.getSurname()); // Modified.
assertEquals(w1.firstName, w1Entity.getFirstName()); // Not modified. assertEquals(w1.firstName, w1Entity.getFirstName()); // Not modified.
w1Entity.getCriterionSatisfactionByCode(w1s2.code); // New. w1Entity.getCriterionSatisfactionByCode(w1s2.code); // New.
assertTrue(datesEquals( // Modified.
w1a1Updated.startDate, Date startDate = w1Entity.getResourcesCostCategoryAssignmentByCode(
w1Entity.getResourcesCostCategoryAssignmentByCode(w1a1.code). w1a1.code).getInitDate().toDateTimeAtStartOfDay().toDate();
getInitDate())); Date endDate = w1Entity.getResourcesCostCategoryAssignmentByCode(
assertTrue(datesEquals( // Not modified. w1a1.code).getEndDate().toDateTimeAtStartOfDay().toDate();
w1a1.endDate,
w1Entity.getResourcesCostCategoryAssignmentByCode(w1a1.code). assertTrue(w1a1Updated.startDate.equals(startDate)); // Modified.
getEndDate())); assertTrue(w1a1.endDate.equals(endDate)); // Not modified.
} }
@ -941,10 +934,10 @@ public class ResourceServiceTest {
private MachineDTO createMachineDTOWithTwoCriterionSatisfactions( private MachineDTO createMachineDTOWithTwoCriterionSatisfactions(
String machineName, String criterionTypeName, String machineName, String criterionTypeName,
String criterionName1, XMLGregorianCalendar startDate1, String criterionName1, Date startDate1,
XMLGregorianCalendar endDate1, Date endDate1,
String criterionName2, XMLGregorianCalendar startDate2, String criterionName2, Date startDate2,
XMLGregorianCalendar endDate2) { Date endDate2) {
MachineDTO machineDTO = new MachineDTO(machineName, "desc"); MachineDTO machineDTO = new MachineDTO(machineName, "desc");
@ -961,8 +954,7 @@ public class ResourceServiceTest {
private MachineDTO createMachineDTOWithTwoCostsAssignments( private MachineDTO createMachineDTOWithTwoCostsAssignments(
String machineName, String costCategoryName, String machineName, String costCategoryName,
XMLGregorianCalendar startDate1, XMLGregorianCalendar endDate1, Date startDate1, Date endDate1, Date startDate2, Date endDate2) {
XMLGregorianCalendar startDate2, XMLGregorianCalendar endDate2) {
MachineDTO machineDTO = new MachineDTO(machineName, "desc"); MachineDTO machineDTO = new MachineDTO(machineName, "desc");
@ -977,34 +969,9 @@ public class ResourceServiceTest {
} }
private XMLGregorianCalendar getDate(int year, int month, int day) { private Date getDate(int year, int month, int day) {
return new LocalDate(year, month, day).toDateTimeAtStartOfDay()
try { .toDate();
return DatatypeFactory.newInstance().newXMLGregorianCalendarDate(
year, month, day, DatatypeConstants.FIELD_UNDEFINED);
} catch (DatatypeConfigurationException e) {
throw new RuntimeException(e);
}
}
private boolean datesEquals(XMLGregorianCalendar date1, Date date2) {
GregorianCalendar date2AsGC = new GregorianCalendar();
date2AsGC.setTime(date2);
return datesEquals(date1.toGregorianCalendar(), date2AsGC);
}
private boolean datesEquals(XMLGregorianCalendar date1, LocalDate date2) {
GregorianCalendar date2AsGC = new GregorianCalendar(
date2.getYear(), date2.getMonthOfYear()-1, date2.getDayOfMonth());
return datesEquals(date1.toGregorianCalendar(), date2AsGC);
} }
public boolean datesEquals(GregorianCalendar date1, public boolean datesEquals(GregorianCalendar date1,

View file

@ -0,0 +1,21 @@
#!/bin/sh
. ./rest-common-env.sh
printf "Login name: "
read loginName
printf "Password: "
read password
if [ "$1" = "--prod" ]; then
baseServiceURL=$PRODUCTION_BASE_SERVICE_URL
certificate=$PRODUCTION_CERTIFICATE
else
baseServiceURL=$DEVELOPMENT_BASE_SERVICE_URL
certificate=$DEVELOPMENT_CERTIFICATE
fi
authorization=`./base64.sh $loginName:$password`
curl -sv -X GET $certificate --header "Authorization: Basic $authorization" \
$baseServiceURL/resources | tidy -xml -i -q -utf8