ItEr45S18CUImportacionPartesTrabajo: Moved methods to convert labels from/to DTOs to a separated class.

This commit is contained in:
Manuel Rego Casasnovas 2010-01-26 21:51:57 +01:00 committed by Javier Moran Rua
parent 857ec6a386
commit e8bcadaa41
2 changed files with 74 additions and 36 deletions

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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 <mrego@igalia.com>
*/
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;
}
}

View file

@ -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<LabelDTO> labels = new HashSet<LabelDTO>();
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));
}
}
}