diff --git a/libreplan-business/src/main/java/org/libreplan/business/costcategories/daos/ITypeOfWorkHoursDAO.java b/libreplan-business/src/main/java/org/libreplan/business/costcategories/daos/ITypeOfWorkHoursDAO.java index 5475dba9d..e61a2aea8 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/costcategories/daos/ITypeOfWorkHoursDAO.java +++ b/libreplan-business/src/main/java/org/libreplan/business/costcategories/daos/ITypeOfWorkHoursDAO.java @@ -46,6 +46,8 @@ public interface ITypeOfWorkHoursDAO extends boolean existsByCode(TypeOfWorkHours typeOfWorkHours); + boolean existsByName(TypeOfWorkHours typeOfWorkHours); + boolean existsTypeWithCodeInAnotherTransaction(String code); List findActive(); diff --git a/libreplan-business/src/main/java/org/libreplan/business/costcategories/daos/TypeOfWorkHoursDAO.java b/libreplan-business/src/main/java/org/libreplan/business/costcategories/daos/TypeOfWorkHoursDAO.java index 176a6328f..587aa4016 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/costcategories/daos/TypeOfWorkHoursDAO.java +++ b/libreplan-business/src/main/java/org/libreplan/business/costcategories/daos/TypeOfWorkHoursDAO.java @@ -167,4 +167,11 @@ public class TypeOfWorkHoursDAO extends IntegrationEntityDAO } } + @Override + public boolean existsByName(TypeOfWorkHours typeOfWorkHours) { + Criteria c = getSession().createCriteria(TypeOfWorkHours.class).add( + Restrictions.eq("name", typeOfWorkHours.getName())); + return c.uniqueResult() != null; + } + } diff --git a/libreplan-business/src/main/java/org/libreplan/business/costcategories/entities/ITypeOfWorkHoursBootstrap.java b/libreplan-business/src/main/java/org/libreplan/business/costcategories/entities/ITypeOfWorkHoursBootstrap.java new file mode 100644 index 000000000..91b02afe7 --- /dev/null +++ b/libreplan-business/src/main/java/org/libreplan/business/costcategories/entities/ITypeOfWorkHoursBootstrap.java @@ -0,0 +1,32 @@ +/* + * This file is part of LibrePlan + * + * Copyright (C) 2011 CafédeRed Solutions, S.L. + * + * 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.libreplan.business.costcategories.entities; + +import org.libreplan.business.IDataBootstrap; + +/** + * Interface for {@link TypeOfWorkHoursBootstrap}. + * + * @author Ignacio Díaz Teijido + */ +public interface ITypeOfWorkHoursBootstrap extends IDataBootstrap { + + void loadRequiredData(); + +} diff --git a/libreplan-business/src/main/java/org/libreplan/business/costcategories/entities/PredefinedTypeOfWorkHours.java b/libreplan-business/src/main/java/org/libreplan/business/costcategories/entities/PredefinedTypeOfWorkHours.java new file mode 100644 index 000000000..a2631a832 --- /dev/null +++ b/libreplan-business/src/main/java/org/libreplan/business/costcategories/entities/PredefinedTypeOfWorkHours.java @@ -0,0 +1,56 @@ +/* + * This file is part of LibrePlan + * + * Copyright (C) 2011 CafédeRed Solutions, S.L. + * + * 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.libreplan.business.costcategories.entities; + +import java.math.BigDecimal; + +/** + * Defines the default {@link TypeOfWorkHours}. + * + * @author Ignacio Díaz Teijido + */ +public enum PredefinedTypeOfWorkHours { + + DEFAULT("Default", 30), OVERTIME("Overtime", 50); + + private TypeOfWorkHours typeOfWorkHours; + + + private PredefinedTypeOfWorkHours(String name, double price) { + typeOfWorkHours = TypeOfWorkHours.create(); + typeOfWorkHours.setName(name); + typeOfWorkHours.setDefaultPrice(new BigDecimal(price).setScale(2)); + } + + public TypeOfWorkHours getTypeOfWorkHours() { + return typeOfWorkHours; + } + + public static boolean contains(TypeOfWorkHours typeOfWorkHours) { + PredefinedTypeOfWorkHours[] types = PredefinedTypeOfWorkHours.values(); + for (PredefinedTypeOfWorkHours each : types) { + if (each.getTypeOfWorkHours().getName() + .equals(typeOfWorkHours.getName())) { + return true; + } + } + return false; + } + +} \ No newline at end of file diff --git a/libreplan-business/src/main/java/org/libreplan/business/costcategories/entities/TypeOfWorkHoursBootstrap.java b/libreplan-business/src/main/java/org/libreplan/business/costcategories/entities/TypeOfWorkHoursBootstrap.java new file mode 100644 index 000000000..7fd2685e4 --- /dev/null +++ b/libreplan-business/src/main/java/org/libreplan/business/costcategories/entities/TypeOfWorkHoursBootstrap.java @@ -0,0 +1,62 @@ +/* + * This file is part of LibrePlan + * + * Copyright (C) 2011 CafédeRed Solutions, S.L. + * + * 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.libreplan.business.costcategories.entities; + +import org.libreplan.business.common.daos.IEntitySequenceDAO; +import org.libreplan.business.common.entities.EntityNameEnum; +import org.libreplan.business.costcategories.daos.ITypeOfWorkHoursDAO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +/** + * Creates the default {@link TypeOfWorkHours}. + * + * @author Ignacio Díaz Teijido + */ +@Component +@Scope("singleton") +public class TypeOfWorkHoursBootstrap implements ITypeOfWorkHoursBootstrap { + + @Autowired + private ITypeOfWorkHoursDAO typeOfWorkHoursDAO; + + @Autowired + private IEntitySequenceDAO entitySequenceDAO; + + @Override + @Transactional + public void loadRequiredData() { + for (PredefinedTypeOfWorkHours predefinedTypeOfWorkHours : PredefinedTypeOfWorkHours + .values()) { + TypeOfWorkHours typeOfWorkHours = predefinedTypeOfWorkHours + .getTypeOfWorkHours(); + if (!typeOfWorkHoursDAO.existsByName(typeOfWorkHours)) { + typeOfWorkHours.setCodeAutogenerated(true); + typeOfWorkHours + .setCode(entitySequenceDAO + .getNextEntityCodeWithoutTransaction(EntityNameEnum.WORK_HOURS_TYPE)); + typeOfWorkHoursDAO.save(typeOfWorkHours); + } + } + } + +}