ItEr49S18AdaptacionServiciosREST: Fixes the LabelServiceREST.
This commit is contained in:
parent
4556532410
commit
8f41cd5836
9 changed files with 194 additions and 63 deletions
|
|
@ -24,6 +24,7 @@ import java.util.Collections;
|
|||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.hibernate.validator.NotEmpty;
|
||||
import org.hibernate.validator.NotNull;
|
||||
|
|
@ -115,4 +116,10 @@ public class Label extends IntegrationEntity {
|
|||
return Registry.getLabelDAO();
|
||||
}
|
||||
|
||||
public void updateUnvalidated(String name) {
|
||||
if (!StringUtils.isBlank(name)) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -141,4 +141,26 @@ public class LabelType extends IntegrationEntity implements Comparable {
|
|||
return !StringUtils.isBlank(name);
|
||||
}
|
||||
|
||||
public void updateUnvalidated(String name) {
|
||||
if (!StringUtils.isBlank(name)) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
public Label getLabelByCode(String code) throws InstanceNotFoundException {
|
||||
|
||||
if (StringUtils.isBlank(code)) {
|
||||
throw new InstanceNotFoundException(code, Label.class.getName());
|
||||
}
|
||||
|
||||
for (Label l : labels) {
|
||||
if (l.getCode().equalsIgnoreCase(StringUtils.trim(code))) {
|
||||
return l;
|
||||
}
|
||||
}
|
||||
|
||||
throw new InstanceNotFoundException(code, Label.class.getName());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,10 +20,15 @@
|
|||
|
||||
package org.navalplanner.ws.labels.impl;
|
||||
|
||||
import static org.navalplanner.web.I18nHelper._;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
|
||||
import org.navalplanner.business.common.exceptions.ValidationException;
|
||||
import org.navalplanner.business.labels.entities.Label;
|
||||
import org.navalplanner.business.labels.entities.LabelType;
|
||||
import org.navalplanner.ws.labels.api.LabelDTO;
|
||||
|
|
@ -83,4 +88,31 @@ public final class LabelConverter {
|
|||
return label;
|
||||
}
|
||||
|
||||
public final static void updateLabelType(LabelType labelType,
|
||||
LabelTypeDTO labelTypeDTO) throws ValidationException {
|
||||
|
||||
/*
|
||||
* 1: Update basic properties in existing label or add new label.
|
||||
*/
|
||||
List<LabelDTO> labelDTOs = labelTypeDTO.labels;
|
||||
for (LabelDTO labelDTO : labelDTOs) {
|
||||
|
||||
/* Step 1.1: requires each label DTO to have a code. */
|
||||
if (StringUtils.isBlank(labelDTO.code)) {
|
||||
throw new ValidationException(_("missing code in a label"));
|
||||
}
|
||||
|
||||
try {
|
||||
Label label = labelType.getLabelByCode(labelDTO.code);
|
||||
label.updateUnvalidated(StringUtils.trim(labelDTO.name));
|
||||
} catch (InstanceNotFoundException e) {
|
||||
labelType.addLabel(toEntity(labelDTO));
|
||||
}
|
||||
}
|
||||
|
||||
/* 2: Update label type basic properties. */
|
||||
labelType.updateUnvalidated(StringUtils.trim(labelTypeDTO.name));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,26 +20,19 @@
|
|||
|
||||
package org.navalplanner.ws.labels.impl;
|
||||
|
||||
import static org.navalplanner.web.I18nHelper._;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.Produces;
|
||||
|
||||
import org.navalplanner.business.common.daos.IIntegrationEntityDAO;
|
||||
import org.navalplanner.business.common.exceptions.ValidationException;
|
||||
import org.navalplanner.business.labels.daos.ILabelTypeDAO;
|
||||
import org.navalplanner.business.labels.entities.LabelType;
|
||||
import org.navalplanner.ws.common.api.InstanceConstraintViolationsDTO;
|
||||
import org.navalplanner.ws.common.api.InstanceConstraintViolationsListDTO;
|
||||
import org.navalplanner.ws.common.impl.ConstraintViolationConverter;
|
||||
import org.navalplanner.ws.common.impl.Util;
|
||||
import org.navalplanner.ws.common.impl.GenericRESTService;
|
||||
import org.navalplanner.ws.common.impl.RecoverableErrorException;
|
||||
import org.navalplanner.ws.labels.api.ILabelService;
|
||||
import org.navalplanner.ws.labels.api.LabelTypeDTO;
|
||||
import org.navalplanner.ws.labels.api.LabelTypeListDTO;
|
||||
|
|
@ -55,7 +48,8 @@ import org.springframework.transaction.annotation.Transactional;
|
|||
@Path("/labels/")
|
||||
@Produces("application/xml")
|
||||
@Service("labelServiceREST")
|
||||
public class LabelServiceREST implements ILabelService {
|
||||
public class LabelServiceREST extends
|
||||
GenericRESTService<LabelType, LabelTypeDTO> implements ILabelService {
|
||||
|
||||
@Autowired
|
||||
private ILabelTypeDAO labelTypeDAO;
|
||||
|
|
@ -64,7 +58,7 @@ public class LabelServiceREST implements ILabelService {
|
|||
@GET
|
||||
@Transactional(readOnly = true)
|
||||
public LabelTypeListDTO getLabelTypes() {
|
||||
return LabelConverter.toDTO(labelTypeDAO.getAll());
|
||||
return new LabelTypeListDTO(findAll());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -73,51 +67,29 @@ public class LabelServiceREST implements ILabelService {
|
|||
@Transactional
|
||||
public InstanceConstraintViolationsListDTO addLabelTypes(
|
||||
LabelTypeListDTO labelTypes) {
|
||||
List<InstanceConstraintViolationsDTO> instanceConstraintViolationsList = new ArrayList<InstanceConstraintViolationsDTO>();
|
||||
Long numItem = new Long(1);
|
||||
Set<String> labelTypeNames = new HashSet<String>();
|
||||
|
||||
for (LabelTypeDTO labelTypeDTO : labelTypes.labelTypes) {
|
||||
InstanceConstraintViolationsDTO instanceConstraintViolationsDTO = null;
|
||||
|
||||
LabelType labelType = LabelConverter.toEntity(labelTypeDTO);
|
||||
|
||||
if (labelType.getName() != null
|
||||
&& labelTypeNames.contains(labelType.getName()
|
||||
.toLowerCase())) {
|
||||
|
||||
instanceConstraintViolationsDTO = InstanceConstraintViolationsDTO
|
||||
.create(Util.generateInstanceConstraintViolationsDTOId(
|
||||
numItem, labelTypeDTO),
|
||||
_("label type name is used by another label "
|
||||
+ "type being imported"));
|
||||
} else {
|
||||
try {
|
||||
labelType.validate();
|
||||
labelTypeDAO.save(labelType);
|
||||
|
||||
if (labelType.getName() != null) {
|
||||
labelTypeNames.add(labelType.getName().toLowerCase());
|
||||
}
|
||||
} catch (ValidationException e) {
|
||||
instanceConstraintViolationsDTO = ConstraintViolationConverter
|
||||
.toDTO(Util
|
||||
.generateInstanceConstraintViolationsDTOId(
|
||||
numItem, labelTypeDTO), e
|
||||
.getInvalidValues());
|
||||
}
|
||||
return save(labelTypes.labelTypes);
|
||||
}
|
||||
|
||||
if (instanceConstraintViolationsDTO != null) {
|
||||
instanceConstraintViolationsList
|
||||
.add(instanceConstraintViolationsDTO);
|
||||
@Override
|
||||
protected IIntegrationEntityDAO<LabelType> getIntegrationEntityDAO() {
|
||||
return labelTypeDAO;
|
||||
}
|
||||
|
||||
numItem++;
|
||||
@Override
|
||||
protected LabelTypeDTO toDTO(LabelType entity) {
|
||||
return LabelConverter.toDTO(entity);
|
||||
}
|
||||
|
||||
return new InstanceConstraintViolationsListDTO(
|
||||
instanceConstraintViolationsList);
|
||||
@Override
|
||||
protected LabelType toEntity(LabelTypeDTO entityDTO)
|
||||
throws ValidationException, RecoverableErrorException {
|
||||
return LabelConverter.toEntity(entityDTO);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateEntity(LabelType entity, LabelTypeDTO entityDTO)
|
||||
throws ValidationException, RecoverableErrorException {
|
||||
LabelConverter.updateLabelType(entity, entityDTO);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ import static org.navalplanner.web.test.ws.common.Util.mustEnd;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.junit.Test;
|
||||
|
|
@ -57,6 +58,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|||
*
|
||||
* @author Manuel Rego Casasnovas <mrego@igalia.com>
|
||||
*/
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { BUSINESS_SPRING_CONFIG_FILE,
|
||||
WEBAPP_SPRING_CONFIG_FILE, WEBAPP_SPRING_CONFIG_TEST_FILE })
|
||||
|
|
@ -78,7 +80,8 @@ public class LabelServiceTest {
|
|||
private LabelType givenLabelTypeStored() {
|
||||
Label label1 = Label.create("label-name-1");
|
||||
Label label2 = Label.create("label-name-2");
|
||||
final LabelType labelType = LabelType.create("label-type-name");
|
||||
final LabelType labelType = LabelType.create("label-type-name"
|
||||
+ UUID.randomUUID());
|
||||
|
||||
labelType.addLabel(label1);
|
||||
labelType.addLabel(label2);
|
||||
|
|
@ -135,7 +138,7 @@ public class LabelServiceTest {
|
|||
public void importValidLabelType() {
|
||||
int previous = labelTypeDAO.getAll().size();
|
||||
|
||||
LabelTypeDTO labelTypeDTO = new LabelTypeDTO("label-type-name",
|
||||
LabelTypeDTO labelTypeDTO = new LabelTypeDTO("label-type-name1",
|
||||
new ArrayList<LabelDTO>());
|
||||
|
||||
List<InstanceConstraintViolationsDTO> instanceConstraintViolationsList = labelService
|
||||
|
|
@ -153,21 +156,24 @@ public class LabelServiceTest {
|
|||
public void importTwoValidLabelType() {
|
||||
int previous = labelTypeDAO.getAll().size();
|
||||
|
||||
LabelTypeDTO labelTypeDTO1 = new LabelTypeDTO("label-type-name1",
|
||||
LabelTypeDTO labelTypeDTO1 = new LabelTypeDTO("label-type-A",
|
||||
new ArrayList<LabelDTO>());
|
||||
LabelTypeDTO labelTypeDTO2 = new LabelTypeDTO("label-type-name2",
|
||||
LabelTypeDTO labelTypeDTO2 = new LabelTypeDTO("label-type-B",
|
||||
new ArrayList<LabelDTO>());
|
||||
|
||||
LabelTypeListDTO labelTypeDTOs = createLabelTypeListDTO(labelTypeDTO1,
|
||||
labelTypeDTO2);
|
||||
|
||||
List<InstanceConstraintViolationsDTO> instanceConstraintViolationsList = labelService
|
||||
.addLabelTypes(new LabelTypeListDTO(Arrays.asList(
|
||||
labelTypeDTO1, labelTypeDTO2))).instanceConstraintViolationsList;
|
||||
.addLabelTypes(labelTypeDTOs).instanceConstraintViolationsList;
|
||||
|
||||
assertThat(instanceConstraintViolationsList.size(), equalTo(0));
|
||||
|
||||
List<LabelType> labelTypes = labelTypeDAO.getAll();
|
||||
assertThat(labelTypes.size(), equalTo(previous + 2));
|
||||
for (LabelType labelType : labelTypes) {
|
||||
assertThat(labelType.getName(), anyOf(equalTo(labelTypeDTO1.name),
|
||||
equalTo(labelTypeDTO2.name)));
|
||||
equalTo(labelTypeDTO2.name), equalTo("label-type-name1")));
|
||||
assertThat(labelType.getLabels().size(), equalTo(0));
|
||||
}
|
||||
}
|
||||
|
|
@ -176,7 +182,7 @@ public class LabelServiceTest {
|
|||
public void importTwoLabelTypeWithRepeatedName() {
|
||||
int previous = labelTypeDAO.getAll().size();
|
||||
|
||||
String labelTypeName = "label-type-name";
|
||||
String labelTypeName = "label-type-nameX";
|
||||
LabelTypeDTO labelTypeDTO1 = new LabelTypeDTO(labelTypeName,
|
||||
new ArrayList<LabelDTO>());
|
||||
LabelTypeDTO labelTypeDTO2 = new LabelTypeDTO(labelTypeName,
|
||||
|
|
@ -201,7 +207,7 @@ public class LabelServiceTest {
|
|||
LabelDTO labelDTO2 = new LabelDTO("label-name-2");
|
||||
List<LabelDTO> labelDTOs = Arrays.asList(labelDTO1, labelDTO2);
|
||||
|
||||
LabelTypeDTO labelTypeDTO = new LabelTypeDTO("label-type-name",
|
||||
LabelTypeDTO labelTypeDTO = new LabelTypeDTO("label-type-nameY",
|
||||
labelDTOs);
|
||||
|
||||
List<InstanceConstraintViolationsDTO> instanceConstraintViolationsList = labelService
|
||||
|
|
@ -210,7 +216,7 @@ public class LabelServiceTest {
|
|||
|
||||
assertThat(labelTypeDAO.getAll().size(), equalTo(previous + 1));
|
||||
|
||||
LabelType labelType = labelTypeDAO.getAll().get(0);
|
||||
LabelType labelType = labelTypeDAO.getAll().get(previous);
|
||||
assertThat(labelType.getName(), equalTo(labelTypeDTO.name));
|
||||
assertThat(labelType.getLabels().size(), equalTo(2));
|
||||
for (Label label : labelType.getLabels()) {
|
||||
|
|
@ -263,4 +269,12 @@ public class LabelServiceTest {
|
|||
assertThat(labelTypeDAO.getAll().size(), equalTo(previous));
|
||||
}
|
||||
|
||||
private LabelTypeListDTO createLabelTypeListDTO(LabelTypeDTO... labelTypes) {
|
||||
List<LabelTypeDTO> labelTypeList = new ArrayList<LabelTypeDTO>();
|
||||
for (LabelTypeDTO c : labelTypes) {
|
||||
labelTypeList.add(c);
|
||||
}
|
||||
return new LabelTypeListDTO(labelTypeList);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,18 @@
|
|||
|
||||
- export-criterion-types.sh (authenticate with wsreader/wsreader)
|
||||
|
||||
* Import label types:
|
||||
|
||||
- import-label-types.sh label-sample.xml
|
||||
(authenticate with wswriter/wswriter)
|
||||
|
||||
- Check the returned errors are consistent with the comments in
|
||||
label-sample.xml.
|
||||
|
||||
* Export label types:
|
||||
|
||||
- export-label-types.sh (authenticate with wsreader/wsreader)
|
||||
|
||||
* Import resources:
|
||||
|
||||
- import-resources.sh resources-sample-mini.xml (or resources-sample.xml)
|
||||
|
|
|
|||
21
scripts/rest-clients/export-label-types.sh
Executable file
21
scripts/rest-clients/export-label-types.sh
Executable 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/labels | tidy -xml -i -q -utf8
|
||||
33
scripts/rest-clients/import-label-types.sh
Executable file
33
scripts/rest-clients/import-label-types.sh
Executable file
|
|
@ -0,0 +1,33 @@
|
|||
#!/bin/sh
|
||||
|
||||
. ./rest-common-env.sh
|
||||
|
||||
printf "Login name: "
|
||||
read loginName
|
||||
printf "Password: "
|
||||
read password
|
||||
|
||||
baseServiceURL=$DEVELOPMENT_BASE_SERVICE_URL
|
||||
certificate=$DEVELOPMENT_CERTIFICATE
|
||||
|
||||
for i in "$@"
|
||||
do
|
||||
if [ "$i" = "--prod" ]; then
|
||||
baseServiceURL=$PRODUCTION_BASE_SERVICE_URL
|
||||
certificate=$PRODUCTION_CERTIFICATE
|
||||
else
|
||||
file=$i
|
||||
fi
|
||||
done
|
||||
|
||||
if [ "$file" = "" ]; then
|
||||
printf "Missing file\n" 1>&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
authorization=`./base64.sh $loginName:$password`
|
||||
|
||||
curl -sv -X POST $certificate -d @$file \
|
||||
--header "Content-type: application/xml" \
|
||||
--header "Authorization: Basic $authorization" \
|
||||
$baseServiceURL/labels | tidy -xml -i -q -utf8
|
||||
18
scripts/rest-clients/label-sample.xml
Normal file
18
scripts/rest-clients/label-sample.xml
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
|
||||
<label-type-list xmlns="http://rest.ws.navalplanner.org">
|
||||
<label-type name="Centro de coste"
|
||||
code="a3636840-33ed-4e25-8ffd-6f98aa72df2a">
|
||||
<label-list>
|
||||
<label name="CC Coruña"
|
||||
code="b2b9cdee-2785-4c03-8e01-cec0a93b61aa" />
|
||||
<label name="CC Cadiz-Malaga"
|
||||
code="e725226b-94f3-41df-88fe-a1855cedc808" />
|
||||
</label-list>
|
||||
</label-type>
|
||||
<label-type name="Producto" code="a3636840-33ed-4e25-8ffd-6f98aa72dfxx">
|
||||
<label-list>
|
||||
<label name="Modelo" code="b2b9cdee-2785-4c03-8e01-cec0a93b61xt"/>
|
||||
<label name="Serie" code="e725226b-94f3-41df-88fe-a1855cedc8x4"/>
|
||||
</label-list>
|
||||
</label-type>
|
||||
</label-type-list>
|
||||
Loading…
Add table
Reference in a new issue