source: trunk/src/org/expeditee/gui/management/ProfileManager.java@ 1465

Last change on this file since 1465 was 1465, checked in by bnemhaus, 4 years ago

Profile names must be valid frameset names in this new world of authentication.

File size: 10.3 KB
Line 
1package org.expeditee.gui.management;
2
3import java.io.File;
4import java.nio.file.Path;
5import java.nio.file.Paths;
6import java.util.ArrayList;
7import java.util.Collection;
8import java.util.List;
9import java.util.Map;
10import java.util.function.Consumer;
11import java.util.stream.Collectors;
12
13import org.expeditee.agents.ExistingFramesetException;
14import org.expeditee.agents.InvalidFramesetNameException;
15import org.expeditee.core.Colour;
16import org.expeditee.core.Point;
17import org.expeditee.gui.Frame;
18import org.expeditee.gui.FrameIO;
19import org.expeditee.gui.MessageBay;
20import org.expeditee.items.Item;
21import org.expeditee.items.ItemUtils;
22import org.expeditee.items.Text;
23import org.expeditee.setting.Setting;
24import org.expeditee.settings.Settings;
25import org.expeditee.settings.UserSettings;
26import org.expeditee.settings.templates.TemplateSettings;
27
28public class ProfileManager {
29
30 private static final String DEFAULT = UserSettings.DEFAULT_PROFILE_NAME;
31 private static final String[] startPages = { "exploratorysearch", "webbrowser" };
32 public static final String USER_NAME_FLAG = "${" + "USER.NAME" + "}";
33
34 public static Frame createProfile(String profileFor, Map<String, Setting> specifiedSettings,
35 Map<String, Consumer<Frame>> notifyWhenGenerated) {
36 ensureDefaultProfile();
37 Frame profileOne = null;
38 profileFor = FrameIO.ConvertToValidFramesetName(profileFor);
39
40 try {
41 Frame profile = FrameIO.CreateFrameset(profileFor, FrameIO.PROFILE_PATH, true);
42 profileOne = profile;
43 profile.setTitle(profileFor + "'s Profile");
44 Frame defaultFrame = FrameIO.LoadFrame(DEFAULT + "1");
45 MessageBay.suppressMessages(true);
46 UserSettings.UserName.set(profileFor);
47 UserSettings.ProfileName.set(FrameIO.ConvertToValidFramesetName(profileFor));
48
49 int lastNumber = FrameIO.getLastNumber(defaultFrame.getFramesetName());
50 for (int i = 1; i <= lastNumber; i++) {
51 // Load in next default, if it doesn't exist continue loop.
52 defaultFrame = FrameIO.LoadFrame(DEFAULT + i);
53 if (defaultFrame == null) { continue; }
54
55 // Create the next next (currently blank) profile frame.
56 // If there is frame gaps in the default (say if there is no 4.exp but there is
57 // a 5.exp) then retain those gaps.
58 // This way copied relative links work.
59 while (profile.getNumber() < defaultFrame.getNumber()) {
60 profile = FrameIO.CreateFrame(profile.getFramesetName(), null, null);
61 }
62 // Ensure we are working from a blank slate.
63 profile.reset();
64 profile.removeAllItems(profile.getAllItems());
65
66 // For each item on defaultFrame:
67 // 1. Set all items to be relatively linked so once copied their links correctly
68 // point to the frame on the created profile rather than the default profile.
69 // 2. Copy item from defaultFrame to the current profile frame being
70 // constructed.
71 // 3. Replace instance of ${USER_NAME} with 'profileFor'
72 // 4. Replace settings values of copied items with those specified in
73 // specifiedSettings (if present)
74 Collection<Item> defaultAllItems = defaultFrame.getAllItems();
75 Collection<Item> allItems = new ArrayList<Item>();
76 for (Item item: defaultAllItems) {
77 Item copyItem = item.copy();
78 allItems.add(copyItem);
79 }
80 profile.addAllItems(allItems);
81
82 for (Item item: profile.getAllItems()) {
83 if (item instanceof Text) {
84 String content = item.getText();
85 item.setText(ResourceUtil.substitute(content, ProfileManager.USER_NAME_FLAG, profileFor));
86 }
87 }
88
89 String category = profile.getTitle();
90 List<String> settingsKeys = null;
91 if (specifiedSettings != null) {
92 settingsKeys = specifiedSettings.keySet().stream().filter(key ->
93 key.startsWith(category)).collect(Collectors.toList());
94 }
95 if (settingsKeys != null) {
96 for (String key: settingsKeys) {
97 Setting setting = specifiedSettings.get(key);
98 String name = setting.getName();
99 Text representation = setting.generateRepresentation(name, profile.getFramesetName());
100 Collection<Text> canditates = profile.getTextItems();
101 canditates.removeIf(text -> !text.getText().startsWith(representation.getText().split(" ")[0]));
102 canditates.forEach(text -> {
103 Point backupPos = text.getPosition();
104 Item.DuplicateItem(representation, text);
105 text.setText(representation.getText());
106 text.setPosition(backupPos);
107 });
108 }
109 }
110 if (notifyWhenGenerated != null && notifyWhenGenerated.containsKey(category)) {
111 notifyWhenGenerated.get(category).accept(profile);
112 }
113
114 FrameIO.SaveFrame(profile);
115 }
116 MessageBay.suppressMessages(false);
117 } catch (InvalidFramesetNameException | ExistingFramesetException e) {
118 String preamble = "Failed to create profile for '" + DEFAULT + "'. ";
119 if (e instanceof InvalidFramesetNameException) {
120 String message = preamble + "Profile names must start and end with a letter and must contain only letters and numbers";
121 MessageBay.errorMessage(message);
122 } else if (e instanceof ExistingFramesetException) {
123 String message = preamble + "A frameset with this name already exists in: " + FrameIO.PROFILE_PATH;
124 MessageBay.errorMessage(message);
125 }
126 }
127
128 return profileOne;
129 }
130
131 /**
132 * Checks if the default profile exists, creates it if it does not.
133 */
134 public static void ensureDefaultProfile() {
135 // If we can load in the default profile, we have nothing to do.
136 Frame defaultFrame = FrameIO.LoadProfile(DEFAULT);
137 if (defaultFrame != null) {
138 return;
139 }
140
141 // We do not have a default profile, generate one.
142 createDefaultProfile();
143 }
144
145 /**
146 * Creates the default profile.
147 */
148 private static void createDefaultProfile() {
149 try {
150 Frame profile = FrameIO.CreateFrameset(DEFAULT, FrameIO.PROFILE_PATH, true);
151
152 // Give the first frame in the new profile an appropriate title.
153 Text titleItem = profile.getTitleItem();
154 if (titleItem != null) {
155 titleItem.setText("Default Profile Frame");
156 }
157
158 // initial position for placing items.
159 int xPos = 300;
160 int yPos = 100;
161
162 // Add documentation links
163 Path helpDirectory = Paths.get(FrameIO.HELP_PATH);
164 File[] helpFramesets = helpDirectory.toFile().listFiles();
165 if (helpFramesets != null) {
166 // Add the title for the help index
167 Text helpPagesTitle = profile.addText(xPos, yPos, "@Expeditee Help", null);
168 helpPagesTitle.setSize(25);
169 helpPagesTitle.setFontStyle("Bold");
170 helpPagesTitle.setFamily("SansSerif");
171 helpPagesTitle.setColor(TemplateSettings.ColorWheel.get()[3]);
172
173 xPos += 25;
174 System.out.println("Installing frameset: ");
175
176 boolean first_item = true;
177 for (File helpFrameset : helpFramesets) {
178 String framesetName = helpFrameset.getName();
179 if (!FrameIO.isValidFramesetName(framesetName)) {
180 continue;
181 }
182
183 if (first_item) {
184 System.out.print(" " + framesetName);
185 first_item = false;
186 } else {
187 System.out.print(", " + framesetName);
188 }
189 System.out.flush();
190
191 Frame indexFrame = FrameIO.LoadFrame(framesetName + '1');
192 // Look through the folder for help index pages
193 if (indexFrame != null && ItemUtils.FindTag(indexFrame.getSortedItems(), "@HelpIndex") != null) {
194 // yPos += spacing;
195 yPos += 30;
196 Text helpLink = profile.addText(xPos, yPos, '@' + indexFrame.getFramesetName(), null);
197 helpLink.setLink(indexFrame.getName());
198 helpLink.setColor(Colour.GREY);
199 }
200 }
201 System.out.println();
202 }
203
204 // Reset position
205 xPos = 50;
206 yPos = 100;
207
208 // Add start pages
209 Path framesetDirectory = Paths.get(FrameIO.FRAME_PATH);
210 File[] startPageFramesets = framesetDirectory.toFile().listFiles();
211 if (startPageFramesets != null) {
212 // Add Start Page title
213 Text startPagesTitle = profile.addText(xPos, yPos, "@Start Pages", null);
214 startPagesTitle.setSize(25);
215 startPagesTitle.setFontStyle("Bold");
216 startPagesTitle.setFamily("SansSerif");
217 startPagesTitle.setColor(TemplateSettings.ColorWheel.get()[3]);
218
219 xPos += 25;
220
221 // Start Pages should be the first frame in its own frameset +
222 // frameset name should be present in FrameUtils.startPages[].
223 for (File startpagesFrameset : startPageFramesets) {
224 String framesetName = startpagesFrameset.getName();
225
226 // Only add link if frameset is a startpage
227 for (int i = 0; i < startPages.length; i++) {
228 if (framesetName.equals(startPages[i])) {
229 Frame indexFrame = FrameIO.LoadFrame(framesetName + '1');
230
231 // Add start page link
232 if (indexFrame != null) {
233 yPos += 30;
234 Text startPageLink = profile.addText(xPos, yPos, '@' + indexFrame.getFramesetName(), null);
235 startPageLink.setLink(indexFrame.getName());
236 startPageLink.setColor(Colour.GREY);
237 }
238 }
239 }
240 }
241 }
242
243 // Add 'default' settings frameset
244 Settings.Init();
245 Text settingsLink = profile.addText(550, 100, "@Settings", null);
246 settingsLink.setSize((float) 25.0);
247 settingsLink.setFamily("SansSerif");
248 settingsLink.setFontStyle("Bold");
249 settingsLink.setColor(Colour.GREY);
250 Settings.generateSettingsTree(settingsLink);
251 System.out.println("@Settings: Default settings generation complete.");
252
253 FrameIO.SaveFrame(profile);
254
255 int lastNumber = FrameIO.getLastNumber(profile.getFramesetName());
256 for (int i = 1; i <= lastNumber; i++) {
257 Frame frame = FrameIO.LoadFrame(DEFAULT + i);
258 frame.setOwner(DEFAULT);
259 for(Item item: frame.getAllItems()) {
260 item.setOwner(DEFAULT);
261 item.setRelativeLink();
262 }
263 frame.setChanged(true);
264 FrameIO.SaveFrame(frame);
265 }
266
267 } catch (InvalidFramesetNameException | ExistingFramesetException e) {
268 String preamble = "Failed to create profile for '" + DEFAULT + "'. ";
269 if (e instanceof InvalidFramesetNameException) {
270 String message = preamble + "Profile names must start and end with a letter and must contain only letters and numbers";
271 MessageBay.errorMessage(message);
272 } else if (e instanceof ExistingFramesetException) {
273 String message = preamble + "A frameset with this name already exists in: " + FrameIO.PROFILE_PATH;
274 MessageBay.errorMessage(message);
275 }
276 return;
277 }
278 }
279}
Note: See TracBrowser for help on using the repository browser.