ItEr45S23CUImportacionTiposEtiquetasEEtiquetas: Import/export labels service implemented.

This commit is contained in:
Manuel Rego Casasnovas 2010-01-28 13:08:30 +01:00 committed by Javier Moran Rua
parent 9dbe83ceaa
commit 1e28b48fb0
5 changed files with 186 additions and 0 deletions

View file

@ -57,6 +57,10 @@ public class Label extends IntegrationEntity {
return create(new Label(name));
}
public static Label create(String code, String name) {
return create(new Label(name), code);
}
protected Label(String name) {
this.name = name;
}

View file

@ -53,6 +53,10 @@ public class LabelType extends IntegrationEntity implements Comparable {
return create(new LabelType(name));
}
public static LabelType create(String code, String name) {
return create(new LabelType(name), code);
}
protected LabelType(String name) {
this.name = name;
}
@ -72,6 +76,7 @@ public class LabelType extends IntegrationEntity implements Comparable {
public void addLabel(Label label) {
Validate.notNull(label);
labels.add(label);
label.setType(this);
}
public void removeLabel(Label label) {
@ -90,4 +95,5 @@ public class LabelType extends IntegrationEntity implements Comparable {
protected ILabelTypeDAO getIntegrationEntityDAO() {
return Registry.getLabelTypeDAO();
}
}

View file

@ -21,6 +21,7 @@
package org.navalplanner.ws.labels.api;
import org.navalplanner.business.labels.entities.Label;
import org.navalplanner.ws.common.api.InstanceConstraintViolationsListDTO;
/**
* Service for managing {@link Label} entities.
@ -29,4 +30,9 @@ import org.navalplanner.business.labels.entities.Label;
*/
public interface ILabelService {
LabelTypeListDTO getLabelTypes();
InstanceConstraintViolationsListDTO addLabelTypes(
LabelTypeListDTO labelTypes);
}

View file

@ -0,0 +1,86 @@
/*
* This file is part of ###PROJECT_NAME###
*
* 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.labels.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.navalplanner.business.labels.entities.Label;
import org.navalplanner.business.labels.entities.LabelType;
import org.navalplanner.ws.labels.api.LabelDTO;
import org.navalplanner.ws.labels.api.LabelTypeDTO;
import org.navalplanner.ws.labels.api.LabelTypeListDTO;
/**
* Converter from/to {@link Label} related entities to/from DTOs.
*
* @author Manuel Rego Casasnovas <mrego@igalia.com>
*/
public final class LabelConverter {
private LabelConverter() {
}
public final static LabelTypeListDTO toDTO(Collection<LabelType> labelTypes) {
List<LabelTypeDTO> labelTypeDTOs = new ArrayList<LabelTypeDTO>();
for (LabelType label : labelTypes) {
labelTypeDTOs.add(toDTO(label));
}
return new LabelTypeListDTO(labelTypeDTOs);
}
public final static LabelTypeDTO toDTO(LabelType labelType) {
List<LabelDTO> labelDTOs = new ArrayList<LabelDTO>();
for (Label label : labelType.getLabels()) {
labelDTOs.add(toDTO(label));
}
return new LabelTypeDTO(labelType.getCode(), labelType.getName(),
labelDTOs);
}
public final static LabelDTO toDTO(Label label) {
return new LabelDTO(label.getCode(), label.getName());
}
public final static LabelType toEntity(LabelTypeDTO labelTypeDTO) {
LabelType labelType = LabelType.create(labelTypeDTO.code,
labelTypeDTO.name);
for (LabelDTO labelDTO : labelTypeDTO.labels) {
labelType.addLabel(toEntity(labelDTO));
}
return labelType;
}
private static Label toEntity(LabelDTO labelDTO) {
Label label = Label.create(labelDTO.code, labelDTO.name);
return label;
}
}

View file

@ -20,11 +20,32 @@
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.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.labels.api.ILabelService;
import org.navalplanner.ws.labels.api.LabelTypeDTO;
import org.navalplanner.ws.labels.api.LabelTypeListDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* REST-based implementation of {@link ILabelService}.
@ -36,4 +57,67 @@ import org.springframework.stereotype.Service;
@Service("labelServiceREST")
public class LabelServiceREST implements ILabelService {
@Autowired
private ILabelTypeDAO labelTypeDAO;
@Override
@GET
@Transactional(readOnly = true)
public LabelTypeListDTO getLabelTypes() {
return LabelConverter.toDTO(labelTypeDAO.getAll());
}
@Override
@POST
@Consumes("application/xml")
@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());
}
}
if (instanceConstraintViolationsDTO != null) {
instanceConstraintViolationsList
.add(instanceConstraintViolationsDTO);
}
numItem++;
}
return new InstanceConstraintViolationsListDTO(
instanceConstraintViolationsList);
}
}