diff --git a/libreplan-business/src/main/java/org/libreplan/business/workreports/entities/PredefinedWorkReportTypes.java b/libreplan-business/src/main/java/org/libreplan/business/workreports/entities/PredefinedWorkReportTypes.java index 1aa8d5281..920ce3640 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/workreports/entities/PredefinedWorkReportTypes.java +++ b/libreplan-business/src/main/java/org/libreplan/business/workreports/entities/PredefinedWorkReportTypes.java @@ -2,6 +2,7 @@ * This file is part of LibrePlan * * Copyright (C) 2011 CafédeRed Solutions, S.L. + * Copyright (C) 2012 Igalia, 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 @@ -19,12 +20,14 @@ package org.libreplan.business.workreports.entities; /** - * Defines the default {@link WorkReportType}. + * Defines the default {@link WorkReportType WorkReportTypes}. * * @author Ignacio Díaz Teijido + * @author Manuel Rego Casasnovas */ public enum PredefinedWorkReportTypes { - DEFAULT("Default", false, false, false); + DEFAULT("Default", false, false, false), + MONTHLY_TIMESHEETS("Monthly timesheets", false, true, false); private WorkReportType workReportType; @@ -41,4 +44,9 @@ public enum PredefinedWorkReportTypes { public WorkReportType getWorkReportType() { return workReportType; } + + public String getName() { + return workReportType.getName(); + } + } diff --git a/libreplan-business/src/main/java/org/libreplan/business/workreports/entities/WorkReportTypeBootstrap.java b/libreplan-business/src/main/java/org/libreplan/business/workreports/entities/WorkReportTypeBootstrap.java index 7f366ecb6..1f80ff776 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/workreports/entities/WorkReportTypeBootstrap.java +++ b/libreplan-business/src/main/java/org/libreplan/business/workreports/entities/WorkReportTypeBootstrap.java @@ -2,6 +2,7 @@ * This file is part of LibrePlan * * Copyright (C) 2011 CafédeRed Solutions, S.L. + * Copyright (C) 2012 Igalia, 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 @@ -18,8 +19,10 @@ */ package org.libreplan.business.workreports.entities; +import org.hibernate.NonUniqueResultException; import org.libreplan.business.common.daos.IEntitySequenceDAO; import org.libreplan.business.common.entities.EntityNameEnum; +import org.libreplan.business.common.exceptions.InstanceNotFoundException; import org.libreplan.business.workreports.daos.IWorkReportTypeDAO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; @@ -27,11 +30,16 @@ import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; /** - * Creates the default {@link WorkReportType}. + * Creates the default {@link WorkReportType WorkReportTypes}.
+ * + * If there is no work report types, it creates a default work report type.
+ * + * Even if there are already some work report types defined, it creates a work + * report type for monthly timesheets if it is not present in the database yet. * * @author Ignacio Díaz Teijido + * @author Manuel Rego Casasnovas */ - @Component @Scope("singleton") public class WorkReportTypeBootstrap implements IWorkReportTypeBootstrap { @@ -48,14 +56,33 @@ public class WorkReportTypeBootstrap implements IWorkReportTypeBootstrap { if (workReportTypeDAO.getWorkReportTypes().size() == 0) { for (PredefinedWorkReportTypes predefinedWorkReportType : PredefinedWorkReportTypes .values()) { - WorkReportType workReportType = predefinedWorkReportType - .getWorkReportType(); - workReportType.setCodeAutogenerated(true); - workReportType - .setCode(entitySequenceDAO - .getNextEntityCodeWithoutTransaction(EntityNameEnum.WORKREPORTTYPE)); - workReportTypeDAO.save(workReportType); + createAndSaveWorkReportType(predefinedWorkReportType); } + } else { + createMonthlyTimesheetsWorkReportTypeIfNeeded(); + } + } + + private void createAndSaveWorkReportType( + PredefinedWorkReportTypes predefinedWorkReportType) { + WorkReportType workReportType = predefinedWorkReportType + .getWorkReportType(); + workReportType.setCodeAutogenerated(true); + workReportType + .setCode(entitySequenceDAO + .getNextEntityCodeWithoutTransaction(EntityNameEnum.WORKREPORTTYPE)); + workReportTypeDAO.save(workReportType); + } + + private void createMonthlyTimesheetsWorkReportTypeIfNeeded() { + try { + workReportTypeDAO + .findUniqueByName(PredefinedWorkReportTypes.MONTHLY_TIMESHEETS + .getName()); + } catch (NonUniqueResultException e) { + throw new RuntimeException(e); + } catch (InstanceNotFoundException e) { + createAndSaveWorkReportType(PredefinedWorkReportTypes.MONTHLY_TIMESHEETS); } }