/* * This file is part of LibrePlan * * Copyright (C) 2013 St. Antoniusziekenhuis * * 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.libreplan.web.logs; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import org.libreplan.business.common.IntegrationEntity; import org.libreplan.business.common.daos.IConfigurationDAO; import org.libreplan.business.common.entities.EntityNameEnum; import org.libreplan.business.common.exceptions.InstanceNotFoundException; import org.libreplan.business.common.exceptions.ValidationException; import org.libreplan.business.logs.daos.IProjectLogDAO; import org.libreplan.business.logs.daos.IRiskLogDAO; import org.libreplan.business.logs.entities.RiskLog; import org.libreplan.business.orders.daos.IOrderDAO; import org.libreplan.business.orders.entities.Order; import org.libreplan.business.users.daos.IUserDAO; import org.libreplan.business.users.entities.User; import org.libreplan.web.common.IntegrationEntityModel; import org.libreplan.web.common.concurrentdetection.OnConcurrentModification; import org.libreplan.web.security.SecurityUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; /** * Model for UI operations related to {@link RiskLog}. * * @author Misha Gozhda <misha@libreplan-enterprise.com> */ @Service @Scope(BeanDefinition.SCOPE_PROTOTYPE) @OnConcurrentModification(goToPage = "/logs/_logs.zul") public class RiskLogModel extends IntegrationEntityModel implements IRiskLogModel { private RiskLog riskLog; @Autowired private IRiskLogDAO riskLogDAO; @Autowired private IProjectLogDAO projectLogDAO; @Autowired private IUserDAO userDAO; @Autowired private IOrderDAO orderDAO; @Autowired private IConfigurationDAO configurationDAO; @Override @Transactional(readOnly = true) public List<RiskLog> getRiskLogs() { return projectLogDAO.getRiskLogs(); } @Override @Transactional(readOnly = true) public List<Order> getOrders() { return orderDAO.getOrders(); } @Override @Transactional(readOnly = true) public List<User> getUsers() { List<User> users = new ArrayList<>(); users.addAll(userDAO.findAll()); return users; } @Override @Transactional(readOnly = true) public void initCreate() { boolean codeGenerated = configurationDAO.getConfiguration().getGenerateCodeForProjectLog(); this.riskLog = RiskLog.create(); if (codeGenerated) { riskLog.setCodeAutogenerated(codeGenerated); setDefaultCode(); try { riskLog.setCreatedBy(userDAO.findByLoginName(SecurityUtils.getSessionUserLoginName())); } catch (InstanceNotFoundException e) { e.printStackTrace(); } } } @Override public void initEdit(RiskLog riskLog) { this.riskLog = riskLog; } @Override public RiskLog getRiskLog() { return this.riskLog; } @Override @Transactional public void confirmSave() throws ValidationException { riskLogDAO.save(riskLog); } @Override public void cancel() { riskLog = null; } @Override @Transactional public void remove(RiskLog riskLog) { try { riskLogDAO.remove(riskLog.getId()); } catch (InstanceNotFoundException e) { throw new RuntimeException(e); } } @Override public List<RiskLog> getByParent(Order order) { return riskLogDAO.getByParent(order); } @Override public void setRisklog(RiskLog log) { this.riskLog = log; } @Override public EntityNameEnum getEntityName() { return EntityNameEnum.RISK_LOG; } @Override public IntegrationEntity getCurrentEntity() { return this.riskLog; } @Override protected Set<IntegrationEntity> getChildren() { return new HashSet<>(); } @Override public Order getOrder() { return riskLog.getOrder(); } @Override public void setOrder(Order order) { if (this.riskLog != null) { RiskLog riskLog = getRiskLog(); riskLog.setOrder(order); } } @Override public User getCreatedBy() { return riskLog.getCreatedBy(); } @Override public void setCreatedBy(User user) { if (this.riskLog != null) { RiskLog riskLog = getRiskLog(); riskLog.setCreatedBy(user); } } }