Java tutorial
/***************************************************************** * This file is part of CCAFS Planning and Reporting Platform. * CCAFS P&R is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * at your option) any later version. * CCAFS P&R 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 General Public License for more details. * You should have received a copy of the GNU General Public License * along with CCAFS P&R. If not, see <http://www.gnu.org/licenses/>. *****************************************************************/ package org.cgiar.ccafs.ap.action.projects; import org.cgiar.ccafs.ap.action.BaseAction; import org.cgiar.ccafs.ap.config.APConstants; import org.cgiar.ccafs.ap.data.manager.HistoryManager; import org.cgiar.ccafs.ap.data.manager.IPElementManager; import org.cgiar.ccafs.ap.data.manager.ProjectContributionOverviewManager; import org.cgiar.ccafs.ap.data.manager.ProjectManager; import org.cgiar.ccafs.ap.data.model.IPElement; import org.cgiar.ccafs.ap.data.model.OutputOverview; import org.cgiar.ccafs.ap.data.model.Project; import org.cgiar.ccafs.ap.validation.projects.ProjectOutputsValidator; import org.cgiar.ccafs.utils.APConfig; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; import com.google.inject.Inject; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author Hernn David Carvajal B. - CIAT/CCAFS */ public class ProjectOutputsAction extends BaseAction { private static final long serialVersionUID = 5246876705706611499L; private static Logger LOG = LoggerFactory.getLogger(ProjectOutputsAction.class); // Managers private ProjectManager projectManager; private ProjectContributionOverviewManager overviewManager; private IPElementManager ipElementManager; private HistoryManager historyManager; // Model private Project project; private int projectID; private List<OutputOverview> previousOverviews; // Validator private ProjectOutputsValidator validator; @Inject public ProjectOutputsAction(APConfig config, ProjectManager projectManager, ProjectContributionOverviewManager overviewManager, IPElementManager ipElementManager, HistoryManager historyManager, ProjectOutputsValidator validator) { super(config); this.projectManager = projectManager; this.overviewManager = overviewManager; this.ipElementManager = ipElementManager; this.historyManager = historyManager; this.validator = validator; } @Override public int getCurrentPlanningYear() { return config.getPlanningCurrentYear(); } public int getMidOutcomeYear() { return config.getMidOutcomeYear(); } public int getMOGIndex(IPElement mog) { return ipElementManager.getMOGIndex(mog); } public Project getProject() { return project; } public int getProjectID() { return projectID; } public boolean isNewProject() { return project.isNew(config.getCurrentPlanningStartDate()); } @Override public void prepare() throws Exception { projectID = Integer .parseInt(StringUtils.trim(this.getRequest().getParameter(APConstants.PROJECT_REQUEST_ID))); // Getting the activity information project = projectManager.getProject(projectID); project.setOutputs(ipElementManager.getProjectOutputs(projectID)); // Remove the outputs duplicated Set<IPElement> outputsTemp = new HashSet<>(project.getOutputs()); project.getOutputs().clear(); project.getOutputs().addAll(outputsTemp); // if (!this.isHttpPost()) { // Get the project outputs from database project.setOutputsOverview(overviewManager.getProjectContributionOverviews(project)); previousOverviews = new ArrayList<>(); for (OutputOverview output : project.getOutputsOverview()) { previousOverviews.add(new OutputOverview(output.getId())); } } // save previous output overviews int evaluatingYear = 0; if (this.getCycleName().equals(APConstants.REPORTING_SECTION)) { evaluatingYear = this.getCurrentReportingYear(); } else { evaluatingYear = this.getCurrentPlanningYear(); } // Getting the Project lessons for this section. this.setProjectLessons(lessonManager.getProjectComponentLesson(projectID, this.getActionName(), evaluatingYear, this.getCycleName())); if (this.getRequest().getMethod().equalsIgnoreCase("post")) { // Clear out the list if it has some element if (project.getOutputsOverview() != null) { project.getOutputsOverview().clear(); } } // Initializing Section Statuses: this.initializeProjectSectionStatuses(project, this.getCycleName()); // Getting the history for this section. this.setHistory(historyManager.getProjectOutputsHistory(projectID)); } @Override public String save() { boolean success = true; if (this.hasProjectPermission("update", project.getId())) { if (!this.isNewProject()) { super.saveProjectLessons(projectID); } // Check if there are output overviews to delete for (OutputOverview overview : previousOverviews) { if (!project.getOutputsOverview().contains(overview)) { success = overviewManager.deleteProjectContributionOverview(overview.getId(), this.getCurrentUser(), this.getJustification()); } } success = success && overviewManager.saveProjectContribution(project, this.getCurrentUser(), this.getJustification()); if (success) { // Get the validation messages and append them to the save message Collection<String> messages = this.getActionMessages(); if (!messages.isEmpty()) { String validationMessage = messages.iterator().next(); this.setActionMessages(null); this.addActionWarning(this.getText("saving.saved") + validationMessage); } else { this.addActionMessage(this.getText("saving.saved")); } return SUCCESS; } else { this.addActionError(this.getText("saving.problem")); LOG.warn("There was a problem saving the project outputs planning."); return BaseAction.INPUT; } } return NOT_AUTHORIZED; } public void setProject(Project project) { this.project = project; } @Override public void validate() { if (save) { validator.validate(this, project, this.getCycleName()); } } }