Add new work report type to be used in monthly timesheets

Modify WorkReportTypeBootstrap to check that the monthly timesheets is always in the database.

FEA: ItEr76S28UserDashboard
This commit is contained in:
Manuel Rego Casasnovas 2012-05-22 10:56:50 +02:00
parent 5e2ad5092b
commit 763b816d16
2 changed files with 46 additions and 11 deletions

View file

@ -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 <ignacio.diaz@cafedered.com>
* @author Manuel Rego Casasnovas <rego@igalia.com>
*/
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();
}
}

View file

@ -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}.<br />
*
* If there is no work report types, it creates a default work report type.<br />
*
* 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 <ignacio.diaz@cafedered.com>
* @author Manuel Rego Casasnovas <rego@igalia.com>
*/
@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);
}
}