-
Notifications
You must be signed in to change notification settings - Fork 0
/
CategoryManager.java
234 lines (212 loc) · 7.68 KB
/
CategoryManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package com.barattoManager.category;
import com.barattoManager.config.AppConfigurator;
import com.barattoManager.exception.AlreadyExistException;
import com.barattoManager.exception.EmptyStringException;
import com.barattoManager.exception.NullCategoryException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
/**
* This class is a <b>Singleton Class</b><br/> used to access from anywhere to the categories.
*/
public final class CategoryManager {
/**
* Category JSOn file
*/
private final File categoriesFile = new File(AppConfigurator.getInstance().getFileName("category_file"));
/**
* {@link ObjectMapper} object, used to parse JSON
*/
private final ObjectMapper objectMapper = new ObjectMapper();
/**
* {@link HashMap} that contain the root category
*/
private final HashMap<String, Category> categoryMap;
/**
* {@link CategoryManager} constructor
*/
private CategoryManager() {
if (categoriesFile.exists()) {
try {
categoryMap = objectMapper.readValue(categoriesFile, new TypeReference<>() {
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}
else {
categoryMap = new HashMap<>();
}
}
/**
* Holder class of instance
*/
private static final class CategoryManagerHolder {
/**
* Instance of {@link CategoryManager}
*/
private static final CategoryManager instance = new CategoryManager();
}
/**
* Method used to create get the {@link CategoryManager} instance.
* This method uses the lazy loading mechanism cause the inner class is loaded only if
* the {@code #getInstance()} method is called.
* Also is thread safe cause every thread read the same {@link Category} instance.
*
* @return The Instance of {@link CategoryManager} class
*/
public static CategoryManager getInstance() {
return CategoryManagerHolder.instance;
}
/**
* Method used to add a new Main category
*
* @param name Name of new category
* @param description Description of the new category
* @throws AlreadyExistException Is thrown is the category or field that are trying to add already exist.
* @throws EmptyStringException Is thrown if the name is an empty string
* @throws NullCategoryException Is thrown if is nt found a category in the map
*/
public void addNewMainCategory(String name, String description) throws AlreadyExistException, EmptyStringException, NullCategoryException {
if (!name.isEmpty() && !(name.trim().length() == 0)) {
if (!categoryMap.containsKey(name.trim().toLowerCase())) {
categoryMap.put(name.toLowerCase(), new Category(name, description));
// adding the default field
for (final JsonNode objNode : AppConfigurator.getInstance().getDefaultField()) {
addNewField(
new ArrayList<>(List.of("root", name.toLowerCase())),
objNode.get("name").asText(),
objNode.get("required").asBoolean()
);
}
saveCategoryMapChange();
categoryMap.get(name.toLowerCase());
}
else {
throw new AlreadyExistException("La categoria che stai creando esiste gia.");
}
}
else {
throw new EmptyStringException("Il nome della categoria non è valido");
}
}
/**
* Method used to add a new subcategory in the category tree.
*
* @param pathOfSubcategory {@link ArrayList} that represent the path of the category
* @param name Name of the new category
* @param description Description of new category
* @throws AlreadyExistException Is thrown if the category that are trying to add already exist.
* @throws EmptyStringException Is thrown if the name is an empty string
* @throws NullCategoryException Is thrown if is nt found a category in the map
*/
public void addNewSubCategory(ArrayList<String> pathOfSubcategory, String name, String description) throws AlreadyExistException, EmptyStringException, NullCategoryException {
if (!name.isEmpty() && !(name.trim().length() == 0)) {
Category category = getCategoryFromPath(pathOfSubcategory);
if (category != null) {
if (!category.getSubCategory().containsKey(name.trim().toLowerCase()) && !Objects.equals(category.getName().trim().toLowerCase(), name.trim().toLowerCase())) {
category.addSubCategory(name, description);
saveCategoryMapChange();
}
else {
throw new AlreadyExistException("La categoria che stai creando esiste gia.");
}
}
else {
throw new NullCategoryException("Non è stata selezionata una categoria.");
}
}
else {
throw new EmptyStringException("Il nome della sotto-categoria non è valido");
}
}
/**
* Recursive method used to add a new field in a category (and every sub category).
*
* @param pathOfCategory {@link ArrayList} that represent the path of the category
* @param name Name of the new field
* @param isRequired If the field is required ({@code true}) otherwise {@code false}
* @throws AlreadyExistException Is thrown if the new field that are trying to add already exist.
* @throws EmptyStringException Is thrown if the name is an empty string
* @throws NullCategoryException Is thrown if is nt found a category in the map
*/
public void addNewField(ArrayList<String> pathOfCategory, String name, boolean isRequired) throws AlreadyExistException, EmptyStringException, NullCategoryException {
if (!name.isEmpty() && !(name.trim().length() == 0)) {
Category category = getCategoryFromPath(pathOfCategory);
if (category != null) {
// Recursive exit condition
if (!category.getSubCategory().isEmpty()) {
if (!category.getCategoryFields().containsKey(name.trim().toLowerCase())) {
// Add the field in the category
category.addNewFields(name, isRequired);
// Recursive block, for each sub-category re-run this method
for (Category subCategory : category.getSubCategory().values()) {
var newPath = new ArrayList<>(pathOfCategory);
newPath.add(subCategory.getName());
addNewField(newPath, name, isRequired);
}
}
else {
throw new AlreadyExistException("Il campo che stai creando esiste già.");
}
}
else {
if (!category.getCategoryFields().containsKey(name.toLowerCase())) {
category.addNewFields(name, isRequired);
}
else {
throw new AlreadyExistException("Il campo che stai creando esiste già.");
}
}
saveCategoryMapChange();
}
else {
throw new NullCategoryException("Non è stata selezionata una categoria.");
}
}
else {
throw new EmptyStringException("Il nome del campo non è valido");
}
}
/**
* Method used to get a {@link Category} object from a categoryPath ({@code ArrayList<String>})
*
* @param pathOfCategory {@link ArrayList} Category path.
* @return {@link Category} object
*/
private Category getCategoryFromPath(ArrayList<String> pathOfCategory) {
Category mainCategory = null;
for (int i = 1; i < pathOfCategory.size(); i++) {
if (i == 1) {
mainCategory = categoryMap.get(pathOfCategory.get(i).toLowerCase());
}
else {
mainCategory = mainCategory.getSubCategory().get(pathOfCategory.get(i).toLowerCase());
}
}
return mainCategory;
}
/**
* Method used to get the {@link HashMap} of root category
* @return {@link HashMap} of root category
*/
public HashMap<String, Category> getCategoryMap() {
return categoryMap;
}
/**
* Method used to save in the json file the {@link #categoryMap} object
*/
private void saveCategoryMapChange() {
try {
objectMapper.writeValue(categoriesFile, categoryMap);
} catch (IOException e) {
e.printStackTrace();
}
}
}