BrightSide Workbench Full Report + Source Code
TaskSettingsSet.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.scheduler.task.settings;
20 
21 import java.io.IOException;
22 import java.util.Map;
23 import java.util.TreeSet;
24 import org.turro.elephant.context.ElephantContext;
25 import org.turro.file.Document;
26 import org.turro.json.IJSONizable;
27 import org.turro.scheduler.task.constraints.Month;
28 import org.turro.scheduler.task.constraints.WeekDay;
29 
34 public class TaskSettingsSet extends TreeSet<TaskSettings> implements IJSONizable {
35 
36  public static final String
37  SCHEDULER_FILE = "/WEB-INF/elephant/conf/tasks/scheduler.json";
38 
39  public static boolean noTasks() {
40  return Document.from(ElephantContext.getRealPath(SCHEDULER_FILE)).length() < 10L;
41  }
42 
43  /* Factory */
44 
45  public static TaskSettingsSet load() throws IOException {
46  Document doc = Document.from(ElephantContext.getRealPath(SCHEDULER_FILE));
47  if(doc.exists()) {
48  return from(doc.content()).addSystemTasks();
49  } else {
50  return new TaskSettingsSet();
51  }
52  }
53 
54  public void save() throws IOException {
55  Document doc = Document.from(ElephantContext.getRealPath(SCHEDULER_FILE));
56  doc.content(toJson());
57  }
58 
59  /* System */
60 
61  private TaskSettingsSet addSystemTasks() {
62  TaskSettings settings = new TaskSettings();
63  settings.setActive(true);
64  settings.setData("YEARLY");
65  settings.setDescription("Yearly Queue");
66  settings.setImplementation("org.turro.mail.queue.QueueSenderTask");
67  settings.getConstraints().getMonths().add(Month.DECEMBER);
68  settings.getConstraints().getDaysOfMonth().add(28);
69  settings.getConstraints().getHours().add(8);
70  add(settings);
71  settings = new TaskSettings();
72  settings.setActive(true);
73  settings.setData("EVERY_SIX_MONTHS");
74  settings.setDescription("Every six months Queue");
75  settings.setImplementation("org.turro.mail.queue.QueueSenderTask");
76  settings.getConstraints().getMonths().add(Month.DECEMBER);
77  settings.getConstraints().getMonths().add(Month.JUNE);
78  settings.getConstraints().getDaysOfMonth().add(28);
79  settings.getConstraints().getHours().add(8);
80  add(settings);
81  settings = new TaskSettings();
82  settings.setActive(true);
83  settings.setData("QUARTERLY");
84  settings.setDescription("Quarterly Queue");
85  settings.setImplementation("org.turro.mail.queue.QueueSenderTask");
86  settings.getConstraints().getMonths().add(Month.DECEMBER);
87  settings.getConstraints().getMonths().add(Month.SEPTEMBER);
88  settings.getConstraints().getMonths().add(Month.JUNE);
89  settings.getConstraints().getMonths().add(Month.MARCH);
90  settings.getConstraints().getDaysOfMonth().add(28);
91  settings.getConstraints().getHours().add(8);
92  add(settings);
93  settings = new TaskSettings();
94  settings.setActive(true);
95  settings.setData("MONTHLY");
96  settings.setDescription("Monthly Queue");
97  settings.setImplementation("org.turro.mail.queue.QueueSenderTask");
98  settings.getConstraints().getDaysOfMonth().add(28);
99  settings.getConstraints().getHours().add(8);
100  add(settings);
101  settings = new TaskSettings();
102  settings.setActive(true);
103  settings.setData("FORTNIGHTLY");
104  settings.setDescription("Fortnightly Queue");
105  settings.setImplementation("org.turro.mail.queue.QueueSenderTask");
106  settings.getConstraints().getDaysOfMonth().add(1);
107  settings.getConstraints().getDaysOfMonth().add(15);
108  settings.getConstraints().getHours().add(8);
109  add(settings);
110  settings = new TaskSettings();
111  settings.setActive(true);
112  settings.setData("WEEKLY");
113  settings.setDescription("Weekly Queue");
114  settings.setImplementation("org.turro.mail.queue.QueueSenderTask");
115  settings.getConstraints().getWeekDays().add(WeekDay.MONDAY);
116  settings.getConstraints().getHours().add(8);
117  add(settings);
118  settings = new TaskSettings();
119  settings.setActive(true);
120  settings.setData("DAILY");
121  settings.setDescription("Daily Queue");
122  settings.setImplementation("org.turro.mail.queue.QueueSenderTask");
123  settings.getConstraints().getHours().add(8);
124  add(settings);
125  settings = new TaskSettings();
126  settings.setActive(true);
127  settings.setData("AS_GENERATED");
128  settings.setDescription("As generated Queue");
129  settings.setImplementation("org.turro.mail.queue.QueueSenderTask");
130  settings.getConstraints().getMinutes().add(0);
131  settings.getConstraints().getMinutes().add(20);
132  settings.getConstraints().getMinutes().add(40);
133  add(settings);
134  settings = new TaskSettings();
135  settings.setActive(false);
136  settings.setDescription("Check SG");
137  settings.setImplementation("org.turro.contacts.util.ContactSGCheckTask");
138  add(settings);
139 
140  return this;
141  }
142 
143  /* IJSONizable */
144 
145  @Override
146  public String toJson() {
147  return toJson(this);
148  }
149 
150  @Override
151  public String toJson(Map<String, Object> properties) {
152  return toJson(this, properties);
153  }
154 
155  public static TaskSettingsSet from(String json) {
156  return IJSONizable.fromJson(json, TaskSettingsSet.class);
157  }
158 
159 }
String toJson(Map< String, Object > properties)
static TaskSettingsSet from(String json)
void setImplementation(String implementation)