diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/impl/LabelConverter.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/impl/LabelConverter.java new file mode 100644 index 000000000..8fb940da3 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/impl/LabelConverter.java @@ -0,0 +1,71 @@ +/* + * 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 . + */ + +package org.navalplanner.ws.common.impl; + +import org.hibernate.NonUniqueResultException; +import org.navalplanner.business.common.Registry; +import org.navalplanner.business.common.exceptions.InstanceNotFoundException; +import org.navalplanner.business.labels.entities.Label; +import org.navalplanner.business.labels.entities.LabelType; +import org.navalplanner.ws.common.api.LabelDTO; + +/** + * Converter from/to {@link Label} entities to/from DTOs. + * + * @author Manuel Rego Casasnovas + */ +public final class LabelConverter { + + private LabelConverter() { + } + + public final static LabelDTO toDTO(Label label) { + return new LabelDTO(label.getName(), label.getType().getName()); + } + + public final static Label forceToEntity(LabelDTO labelDTO) { + LabelType labelType = null; + try { + labelType = Registry.getLabelTypeDAO().findUniqueByName( + labelDTO.type); + } catch (NonUniqueResultException e) { + throw new RuntimeException(e); + } catch (InstanceNotFoundException e) { + labelType = LabelType.create(labelDTO.type); + /* + * "validate" method avoids that "labelType" goes to the Hibernate's + * session if "labelType" is not valid. + */ + labelType.validate(); + Registry.getLabelTypeDAO().save(labelType); + } + + Label label = Registry.getLabelDAO().findByNameAndType(labelDTO.name, + labelType); + if (label == null) { + label = Label.create(labelDTO.name); + label.setType(labelType); + } + + return label; + } + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/impl/OrderElementConverter.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/impl/OrderElementConverter.java index a057e4e04..6ddfb40ca 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/impl/OrderElementConverter.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/common/impl/OrderElementConverter.java @@ -30,7 +30,6 @@ import java.util.HashSet; import java.util.List; import java.util.Set; -import org.hibernate.NonUniqueResultException; import org.joda.time.LocalDate; import org.navalplanner.business.advance.entities.AdvanceMeasurement; import org.navalplanner.business.advance.entities.DirectAdvanceAssignment; @@ -40,7 +39,6 @@ import org.navalplanner.business.calendars.entities.BaseCalendar; import org.navalplanner.business.common.Registry; import org.navalplanner.business.common.exceptions.InstanceNotFoundException; import org.navalplanner.business.labels.entities.Label; -import org.navalplanner.business.labels.entities.LabelType; import org.navalplanner.business.materials.entities.Material; import org.navalplanner.business.materials.entities.MaterialAssignment; import org.navalplanner.business.materials.entities.MaterialCategory; @@ -91,7 +89,7 @@ public final class OrderElementConverter { Set labels = new HashSet(); if (configuration.isLabels()) { for (Label label : orderElement.getLabels()) { - labels.add(toDTO(label)); + labels.add(LabelConverter.toDTO(label)); } } @@ -208,10 +206,6 @@ public final class OrderElementConverter { .getUnitPrice(), materialAssignment.getEstimatedAvailability()); } - public final static LabelDTO toDTO(Label label) { - return new LabelDTO(label.getName(), label.getType().getName()); - } - public final static HoursGroupDTO toDTO(HoursGroup hoursGroup, ConfigurationOrderElementConverter configuration) { ResourceEnumDTO resourceType = ResourceEnumConverter.toDTO(hoursGroup @@ -376,7 +370,7 @@ public final class OrderElementConverter { if (configuration.isLabels()) { for (LabelDTO labelDTO : orderElementDTO.labels) { - orderElement.addLabel(toEntity(labelDTO)); + orderElement.addLabel(LabelConverter.forceToEntity(labelDTO)); } } @@ -448,33 +442,6 @@ public final class OrderElementConverter { return materialAssignment; } - public final static Label toEntity(LabelDTO labelDTO) { - LabelType labelType = null; - try { - labelType = Registry.getLabelTypeDAO().findUniqueByName( - labelDTO.type); - } catch (NonUniqueResultException e) { - throw new RuntimeException(e); - } catch (InstanceNotFoundException e) { - labelType = LabelType.create(labelDTO.type); - /* - * "validate" method avoids that "labelType" goes to the Hibernate's - * session if "labelType" is not valid. - */ - labelType.validate(); - Registry.getLabelTypeDAO().save(labelType); - } - - Label label = Registry.getLabelDAO().findByNameAndType(labelDTO.name, - labelType); - if (label == null) { - label = Label.create(labelDTO.name); - label.setType(labelType); - } - - return label; - } - public final static HoursGroup toEntity(HoursGroupDTO hoursGroupDTO, ConfigurationOrderElementConverter configuration) { ResourceEnum resourceType = ResourceEnumConverter @@ -569,7 +536,7 @@ public final class OrderElementConverter { if (configuration.isLabels()) { for (LabelDTO labelDTO : orderElementDTO.labels) { if (!orderElement.containsLabel(labelDTO.name, labelDTO.type)) { - orderElement.addLabel(toEntity(labelDTO)); + orderElement.addLabel(LabelConverter.forceToEntity(labelDTO)); } } }