001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ui.apps.scheduler;
029
030import org.opencms.configuration.CmsSchedulerConfiguration;
031import org.opencms.main.CmsContextInfo;
032import org.opencms.main.OpenCms;
033import org.opencms.scheduler.CmsScheduleManager;
034import org.opencms.scheduler.CmsScheduledJobInfo;
035import org.opencms.scheduler.CmsSchedulerException;
036import org.opencms.security.CmsRoleViolationException;
037import org.opencms.ui.A_CmsUI;
038import org.opencms.ui.CmsVaadinUtils;
039import org.opencms.ui.FontOpenCms;
040import org.opencms.ui.apps.A_CmsWorkplaceApp;
041import org.opencms.ui.apps.I_CmsCRUDApp;
042import org.opencms.ui.components.CmsBasicDialog;
043import org.opencms.ui.components.CmsBasicDialog.DialogWidth;
044import org.opencms.ui.components.CmsErrorDialog;
045import org.opencms.ui.components.CmsToolBar;
046import org.opencms.util.CmsStringUtil;
047
048import java.util.LinkedHashMap;
049import java.util.List;
050
051import com.vaadin.ui.Button;
052import com.vaadin.ui.Button.ClickEvent;
053import com.vaadin.ui.Button.ClickListener;
054import com.vaadin.ui.Component;
055import com.vaadin.ui.Window;
056
057/**
058 * The scheduled jobs manager app.<p>
059 */
060public class CmsJobManagerApp extends A_CmsWorkplaceApp implements I_CmsCRUDApp<CmsScheduledJobInfo> {
061
062    /** Parameter copy. */
063    public static final String PARAM_COPY = "copy";
064
065    /** Parameter job id. */
066    public static final String PARAM_JOB_ID = "jobId";
067
068    /** Path name edit. */
069    public static final String PATH_NAME_EDIT = "edit";
070
071    /** Table containing the jobs. */
072    protected CmsJobTable m_jobTable;
073
074    /** The current dialog window. */
075    protected Window m_dialogWindow;
076
077    /**
078     * Closes the currently opened window.<p>
079     *
080     * @param updateTable <code>true</code> to update the jobs table
081     */
082    public void closeDialogWindow(boolean updateTable) {
083
084        if (m_dialogWindow != null) {
085            m_dialogWindow.close();
086            m_dialogWindow = null;
087        }
088        if (updateTable) {
089            m_jobTable.reloadJobs();
090        }
091    }
092
093    /**
094     * @see org.opencms.ui.apps.I_CmsCRUDApp#createElement(java.lang.Object)
095     */
096    public void createElement(CmsScheduledJobInfo element) {
097
098        writeElement(element);
099
100    }
101
102    /**
103     * @see org.opencms.ui.apps.I_CmsCRUDApp#defaultAction(java.lang.String)
104     */
105    public void defaultAction(String elelemntId) {
106
107        openEditDialog(elelemntId, false);
108
109    }
110
111    /**
112     * @see org.opencms.ui.apps.I_CmsCRUDApp#deleteElements(java.util.List)
113     */
114    public void deleteElements(List<String> jobIds) {
115
116        try {
117            for (String jobId : jobIds) {
118                OpenCms.getScheduleManager().unscheduleJob(A_CmsUI.getCmsObject(), jobId);
119            }
120            OpenCms.writeConfiguration(CmsSchedulerConfiguration.class);
121        } catch (CmsRoleViolationException e) {
122            CmsErrorDialog.showErrorDialog(e);
123        }
124
125    }
126
127    /**
128     * @see org.opencms.ui.apps.I_CmsCRUDApp#getAllElements()
129     */
130    public List<CmsScheduledJobInfo> getAllElements() {
131
132        return OpenCms.getScheduleManager().getJobs();
133    }
134
135    /**
136     * @see org.opencms.ui.apps.I_CmsCRUDApp#getElement(java.lang.String)
137     */
138    public CmsScheduledJobInfo getElement(String elementId) {
139
140        return OpenCms.getScheduleManager().getJob(elementId);
141    }
142
143    /**
144     * Creates the edit view for the given job id.<p>
145     *
146     * @param jobId the id of the job to edit, or null to create a new job
147     * @param copy <code>true</code> to create a copy of the given job
148     *
149     * @return the edit view
150     */
151    public CmsJobEditView openEditDialog(String jobId, boolean copy) {
152
153        if (m_dialogWindow != null) {
154            m_dialogWindow.close();
155        }
156        m_dialogWindow = CmsBasicDialog.prepareWindow(DialogWidth.wide);
157        CmsScheduledJobInfo job = null;
158        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(jobId)) {
159            job = getElement(jobId);
160        }
161        CmsScheduledJobInfo jobCopy;
162        if (job == null) {
163            jobCopy = new CmsScheduledJobInfo();
164            jobCopy.setContextInfo(new CmsContextInfo());
165            m_dialogWindow.setCaption(
166                CmsVaadinUtils.getMessageText(
167                    org.opencms.workplace.tools.scheduler.Messages.GUI_NEWJOB_ADMIN_TOOL_NAME_0));
168        } else {
169            jobCopy = job.clone();
170            jobCopy.setActive(job.isActive());
171            if (copy) {
172                jobCopy.clearId();
173                m_dialogWindow.setCaption(
174                    CmsVaadinUtils.getMessageText(
175                        org.opencms.ui.Messages.GUI_SCHEDULER_TITLE_COPY_1,
176                        job.getJobName()));
177            } else {
178                m_dialogWindow.setCaption(
179                    CmsVaadinUtils.getMessageText(
180                        org.opencms.workplace.tools.scheduler.Messages.GUI_JOBS_LIST_ACTION_EDIT_NAME_0));
181            }
182        }
183
184        CmsJobEditView editPanel = new CmsJobEditView(this, jobCopy);
185        editPanel.loadFromBean(jobCopy);
186        m_dialogWindow.setContent(editPanel);
187        A_CmsUI.get().addWindow(m_dialogWindow);
188        m_dialogWindow.center();
189
190        return editPanel;
191    }
192
193    /**
194     * Restores the main view after leaving the editing mode.<p>
195     */
196    public void restoreMainView() {
197
198        openSubView("", true);
199    }
200
201    /**
202     * Executes the given schedule job.<p>
203     *
204     * @param job to be executed
205     */
206    public void runJob(CmsScheduledJobInfo job) {
207
208        CmsScheduleManager scheduler = OpenCms.getScheduleManager();
209        scheduler.executeDirectly(job.getId());
210
211    }
212
213    /**
214     * @see org.opencms.ui.apps.I_CmsCRUDApp#writeElement(java.lang.Object)
215     */
216    public void writeElement(CmsScheduledJobInfo jobInfo) {
217
218        // schedule the edited job
219        try {
220            OpenCms.getScheduleManager().scheduleJob(A_CmsUI.getCmsObject(), jobInfo);
221        } catch (CmsRoleViolationException | CmsSchedulerException e) {
222            //
223        }
224        // update the XML configuration
225        OpenCms.writeConfiguration(CmsSchedulerConfiguration.class);
226
227    }
228
229    /**
230     * @see org.opencms.ui.apps.A_CmsWorkplaceApp#getBreadCrumbForState(java.lang.String)
231     */
232    @Override
233    protected LinkedHashMap<String, String> getBreadCrumbForState(String state) {
234
235        LinkedHashMap<String, String> crumbs = new LinkedHashMap<String, String>();
236        crumbs.put(
237            "",
238            CmsVaadinUtils.getMessageText(org.opencms.workplace.tools.scheduler.Messages.GUI_JOBS_ADMIN_TOOL_NAME_0));
239        return crumbs;
240    }
241
242    /**
243     * @see org.opencms.ui.apps.A_CmsWorkplaceApp#getComponentForState(java.lang.String)
244     */
245    @Override
246    protected Component getComponentForState(String state) {
247
248        Button addJob = CmsToolBar.createButton(
249            FontOpenCms.WAND,
250            CmsVaadinUtils.getMessageText(org.opencms.workplace.tools.scheduler.Messages.GUI_NEWJOB_ADMIN_TOOL_NAME_0));
251        addJob.addClickListener(new ClickListener() {
252
253            private static final long serialVersionUID = 1L;
254
255            public void buttonClick(ClickEvent event) {
256
257                openEditDialog(null, false);
258            }
259        });
260        m_uiContext.addToolbarButton(addJob);
261        m_rootLayout.setMainHeightFull(true);
262        CmsJobTable table = getJobTable();
263        table.reloadJobs();
264        return table;
265    }
266
267    /**
268     * Returns the job table instance.<p>
269     *
270     * @return the job table instance
271     */
272    protected CmsJobTable getJobTable() {
273
274        if (m_jobTable == null) {
275            m_jobTable = new CmsJobTable(this);
276            m_jobTable.setWidth("100%");
277        }
278        return m_jobTable;
279    }
280
281    /**
282     * @see org.opencms.ui.apps.A_CmsWorkplaceApp#getSubNavEntries(java.lang.String)
283     */
284    @Override
285    protected List<NavEntry> getSubNavEntries(String state) {
286
287        return null;
288    }
289
290}