ItEr60S04ValidacionEProbasFuncionaisItEr59S04: Reverted changes in a previous commit coming back to XMLGregorianCalendar for resources service.

This reverts commit "bafae163". But it does not revert the whole commit, just the changes related with the data type of the date fields in the resource service.
This commit is contained in:
Manuel Rego Casasnovas 2010-06-28 00:19:41 +02:00
parent 6637da457c
commit 01219e9e6a
8 changed files with 236 additions and 95 deletions

View file

@ -66,4 +66,4 @@ public class ResourceConverter implements IConverter<Resource> {
return asString(getType().cast(entity));
}
}
}

View file

@ -0,0 +1,105 @@
/*
* 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.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
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());
}
}
/**
* It converts a <code>Date</code> to a <code>XMLGregorianCalendar</code>
* representing a <code>xsd:date</code> XML type.<br/>
* <br/>
*
* If the date passed as a parameter is <code>null</code>, it also returns
* <code>null</code>.
*
* @throws DatatypeConfigurationException
*/
public final static XMLGregorianCalendar toXMLGregorianCalendar(Date date) {
if (date == null) {
return null;
} else {
LocalDate localDate = LocalDate.fromDateFields(date);
DatatypeFactory factory;
try {
factory = DatatypeFactory.newInstance();
} catch (DatatypeConfigurationException e) {
throw new RuntimeException(e);
}
return factory.newXMLGregorianCalendarDate(localDate.getYear(),
localDate.getMonthOfYear(), localDate.getDayOfMonth(),
DatatypeConstants.FIELD_UNDEFINED);
}
}
}

View file

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

View file

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

View file

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

View file

@ -27,7 +27,6 @@ import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.joda.time.LocalDate;
import org.navalplanner.business.calendars.entities.CalendarAvailability;
import org.navalplanner.business.calendars.entities.ResourceCalendar;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
@ -41,6 +40,7 @@ import org.navalplanner.business.resources.entities.Resource;
import org.navalplanner.business.resources.entities.Worker;
import org.navalplanner.ws.calendars.api.BaseCalendarDTO;
import org.navalplanner.ws.calendars.impl.CalendarConverter;
import org.navalplanner.ws.common.impl.DateConverter;
import org.navalplanner.ws.common.impl.InstanceNotFoundRecoverableErrorException;
import org.navalplanner.ws.common.impl.RecoverableErrorException;
import org.navalplanner.ws.resources.api.CalendarAvailabilityDTO;
@ -166,8 +166,9 @@ public class ResourceConverter {
StringUtils.trim(criterionSatisfactionDTO.code),
StringUtils.trim(criterionSatisfactionDTO.criterionTypeName),
StringUtils.trim(criterionSatisfactionDTO.criterionName),
resource, criterionSatisfactionDTO.startDate,
criterionSatisfactionDTO.endDate);
resource,
DateConverter.toDate(criterionSatisfactionDTO.startDate),
DateConverter.toDate(criterionSatisfactionDTO.endDate));
} catch (InstanceNotFoundException e) {
@ -253,14 +254,11 @@ public class ResourceConverter {
}
try {
LocalDate startDate = (assignmentDTO.startDate == null) ? null
: LocalDate.fromDateFields(assignmentDTO.startDate);
LocalDate endDate = (assignmentDTO.endDate == null) ? null
: LocalDate.fromDateFields(assignmentDTO.endDate);
return ResourcesCostCategoryAssignment.createUnvalidated(
assignmentDTO.code, StringUtils
.trim(assignmentDTO.costCategoryName), resource,
startDate, endDate);
assignmentDTO.code,
StringUtils.trim(assignmentDTO.costCategoryName), resource,
DateConverter.toLocalDate(assignmentDTO.startDate),
DateConverter.toLocalDate(assignmentDTO.endDate));
} catch (InstanceNotFoundException e) {
throw new InstanceNotFoundRecoverableErrorException(
COST_CATEGORY_ENTITY_TYPE, e.getKey().toString());
@ -342,8 +340,8 @@ public class ResourceConverter {
criterionSatisfaction.updateUnvalidated(
StringUtils.trim(criterionSatisfactionDTO.criterionTypeName),
StringUtils.trim(criterionSatisfactionDTO.criterionName),
criterionSatisfactionDTO.startDate,
criterionSatisfactionDTO.endDate);
DateConverter.toDate(criterionSatisfactionDTO.startDate),
DateConverter.toDate(criterionSatisfactionDTO.endDate));
} catch (InstanceNotFoundException e) {
@ -391,13 +389,10 @@ public class ResourceConverter {
ResourcesCostCategoryAssignmentDTO i) {
try {
LocalDate startDate = (i.startDate == null) ? null : LocalDate
.fromDateFields(i.startDate);
LocalDate endDate = (i.endDate == null) ? null : LocalDate
.fromDateFields(i.endDate);
assignment.updateUnvalidated(
StringUtils.trim(i.costCategoryName),
startDate, endDate);
DateConverter.toLocalDate(i.startDate),
DateConverter.toLocalDate(i.endDate));
} catch (InstanceNotFoundException e) {
throw new InstanceNotFoundRecoverableErrorException(
COST_CATEGORY_ENTITY_TYPE, e.getKey().toString());
@ -463,9 +458,11 @@ public class ResourceConverter {
CriterionSatisfaction criterionSatisfaction) {
return new CriterionSatisfactionDTO(criterionSatisfaction.getCode(),
criterionSatisfaction.getCriterion().getType().getName(),
criterionSatisfaction.getCriterion().getName(),
criterionSatisfaction.getStartDate(), criterionSatisfaction
.getEndDate());
criterionSatisfaction.getCriterion().getName(), DateConverter
.toXMLGregorianCalendar(criterionSatisfaction
.getStartDate()), DateConverter
.toXMLGregorianCalendar(criterionSatisfaction
.getEndDate()));
}
private static ResourcesCostCategoryAssignmentDTO toDTO(
@ -480,7 +477,8 @@ public class ResourceConverter {
return new ResourcesCostCategoryAssignmentDTO(
resourcesCostCategoryAssignment.getCode(),
resourcesCostCategoryAssignment.getCostCategory().getName(),
initDate, endDate);
DateConverter.toXMLGregorianCalendar(initDate), DateConverter
.toXMLGregorianCalendar(endDate));
}
public static ResourceCalendarDTO toDTO(ResourceCalendar calendar) {

View file

@ -60,6 +60,42 @@ public class ResourceServiceREST
@Autowired
private IResourceDAO resourceDAO;
@Override
@POST
@Consumes("application/xml")
public InstanceConstraintViolationsListDTO addResources(
ResourceListDTO resources) {
return save(resources.resources);
}
@Override
protected Resource toEntity(ResourceDTO entityDTO)
throws ValidationException, RecoverableErrorException {
return ResourceConverter.toEntity(entityDTO);
}
@Override
protected ResourceDTO toDTO(Resource entity) {
return ResourceConverter.toDTO(entity);
}
@Override
protected IIntegrationEntityDAO<Resource> getIntegrationEntityDAO() {
return resourceDAO;
}
@Override
protected void updateEntity(Resource entity, ResourceDTO entityDTO)
throws ValidationException, RecoverableErrorException {
ResourceConverter.updateResource(entity, entityDTO);
}
@Autowired
private IWorkerDAO workerDAO;
@ -81,40 +117,4 @@ public class ResourceServiceREST
return toDTO(result);
}
@Override
@POST
@Consumes("application/xml")
public InstanceConstraintViolationsListDTO addResources(
ResourceListDTO resources) {
return save(resources.resources);
}
@Override
protected Resource toEntity(ResourceDTO entityDTO)
throws ValidationException, RecoverableErrorException {
return ResourceConverter.toEntity(entityDTO);
}
@Override
protected ResourceDTO toDTO(Resource entity) {
return ResourceConverter.toDTO(entity);
}
@Override
protected IIntegrationEntityDAO<Resource> getIntegrationEntityDAO() {
return resourceDAO;
}
@Override
protected void updateEntity(Resource entity, ResourceDTO entityDTO)
throws ValidationException, RecoverableErrorException {
ResourceConverter.updateResource(entity, entityDTO);
}
}

View file

@ -41,6 +41,11 @@ import java.util.Date;
import java.util.GregorianCalendar;
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.junit.Before;
import org.junit.Test;
@ -785,12 +790,13 @@ public class ResourceServiceTest {
Machine m1Entity = machineDAO.findByCode(m1.code);
assertEquals(m1Updated.name, m1Entity.getName()); // Modified.
assertEquals(m1.description, m1Entity.getDescription()); // Not
// modified.
assertTrue(m1s1Updated.startDate.equals(m1Entity
.getCriterionSatisfactionByCode(m1s1.code).getStartDate()));
assertTrue(m1s1.endDate.equals(m1Entity.getCriterionSatisfactionByCode(
m1s1.code).getEndDate())); // Not modified.
assertEquals(m1.description, m1Entity.getDescription()); //Not modified.
assertTrue(datesEquals( // Modified.
m1s1Updated.startDate,
m1Entity.getCriterionSatisfactionByCode(m1s1.code).getStartDate()));
assertTrue(datesEquals( // Not modified.
m1s1.endDate,
m1Entity.getCriterionSatisfactionByCode(m1s1.code).getEndDate()));
m1Entity.getResourcesCostCategoryAssignmentByCode(m1a2.code); // New.
/* Test worker update. */
@ -799,14 +805,14 @@ public class ResourceServiceTest {
assertEquals(w1Updated.surname, w1Entity.getSurname()); // Modified.
assertEquals(w1.firstName, w1Entity.getFirstName()); // Not modified.
w1Entity.getCriterionSatisfactionByCode(w1s2.code); // New.
Date startDate = w1Entity.getResourcesCostCategoryAssignmentByCode(
w1a1.code).getInitDate().toDateTimeAtStartOfDay().toDate();
Date endDate = w1Entity.getResourcesCostCategoryAssignmentByCode(
w1a1.code).getEndDate().toDateTimeAtStartOfDay().toDate();
assertTrue(w1a1Updated.startDate.equals(startDate)); // Modified.
assertTrue(w1a1.endDate.equals(endDate)); // Not modified.
assertTrue(datesEquals( // Modified.
w1a1Updated.startDate,
w1Entity.getResourcesCostCategoryAssignmentByCode(w1a1.code).
getInitDate()));
assertTrue(datesEquals( // Not modified.
w1a1.endDate,
w1Entity.getResourcesCostCategoryAssignmentByCode(w1a1.code).
getEndDate()));
}
@ -927,10 +933,10 @@ public class ResourceServiceTest {
private MachineDTO createMachineDTOWithTwoCriterionSatisfactions(
String machineName, String criterionTypeName,
String criterionName1, Date startDate1,
Date endDate1,
String criterionName2, Date startDate2,
Date endDate2) {
String criterionName1, XMLGregorianCalendar startDate1,
XMLGregorianCalendar endDate1,
String criterionName2, XMLGregorianCalendar startDate2,
XMLGregorianCalendar endDate2) {
MachineDTO machineDTO = new MachineDTO(machineName, "desc");
@ -947,7 +953,8 @@ public class ResourceServiceTest {
private MachineDTO createMachineDTOWithTwoCostsAssignments(
String machineName, String costCategoryName,
Date startDate1, Date endDate1, Date startDate2, Date endDate2) {
XMLGregorianCalendar startDate1, XMLGregorianCalendar endDate1,
XMLGregorianCalendar startDate2, XMLGregorianCalendar endDate2) {
MachineDTO machineDTO = new MachineDTO(machineName, "desc");
@ -962,9 +969,34 @@ public class ResourceServiceTest {
}
private Date getDate(int year, int month, int day) {
return new LocalDate(year, month, day).toDateTimeAtStartOfDay()
.toDate();
private XMLGregorianCalendar getDate(int year, int month, int day) {
try {
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,