BrightSide Workbench Full Report + Source Code
WebResources.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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.elephant.web.context;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.nio.file.Path;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29 import java.util.TreeMap;
30 import java.util.TreeSet;
31 import java.util.logging.Level;
32 import java.util.logging.Logger;
33 import org.turro.string.Strings;
34 import org.turro.elephant.context.ElephantContext;
35 import org.turro.elephant.impl.util.FileUtil;
36 import org.turro.elephant.web.ElContext;
37 import org.turro.plugin.contacts.IContact;
38 
43 public class WebResources {
44 
45  private final Path folder;
46 
47  // Base name
48  private Map<String, WebLocaleResource> resources = new TreeMap<>();
49 
50  public WebResources(Path folder) {
51  this.folder = folder;
52  populate();
53  }
54 
55  public List<String> globalResourceNames() {
56  return resourceNamesForLang("");
57  }
58 
59  public List<String> resourceNamesForLang(String lang) {
60  List<String> names = new ArrayList<>();
61  resources.values().forEach(wlr -> {
62  String name = wlr.resourceNameForLang(lang);
63  if(!Strings.isBlank(name)) names.add(name);
64  });
65  return names;
66  }
67 
68  public Set<String> getBaseNames() {
69  return resources.keySet();
70  }
71 
72  public Set<String> getLangs() {
73  Set<String> langs = new TreeSet<>();
74  resources.values().forEach(v -> langs.addAll(v.getLangs()));
75  return langs;
76  }
77 
78  public File getResource(String base, String lang) {
79  WebResource wr = webResource(base, lang);
80  return wr != null ? wr.getResource() : null;
81  }
82 
83  public Set<String> getVersions(String base, String lang) {
84  WebResource wr = webResource(base, lang);
85  return wr != null ? new TreeSet(wr.getEditables().keySet()).descendingSet() : Collections.EMPTY_SET;
86  }
87 
88  public File getEditable(String base, String lang, String version) {
89  if(!Strings.isBlank(version)) {
90  WebResource wr = webResource(base, lang);
91  return wr != null ? wr.getEditables().get(version) : null;
92  }
93  return null;
94  }
95 
97  resources.putIfAbsent(base, resource);
98  return resources.get(base);
99  }
100 
101  public void saveContent(Path folder, IContact contact, String base, String lang, String wiki, String html, boolean autoremove) {
102  try {
103  WebResourceParts wrp = WebResourceParts.editable(contact, base, lang);
104  File editable = wrp.getEditableFile(folder, contact);
105  File resource = wrp.getResourceFile(folder);
106  FileUtil.setContent(editable, wiki);
107  FileUtil.setContent(resource, html);
108  if(autoremove) {
109  WebResource wr = webResource(base, lang);
110  if(wr != null) wr.cleanEditables(wrp.getModified());
111  }
112  } catch (IOException ex) {
113  Logger.getLogger(WebResources.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
114  }
115  }
116 
117  private void populate() {
118  for(File file : folder.toFile().listFiles()) {
119  if(file.isFile() && !ElContext.isInternal(file)) {
121  if(!wrp.isEmpty()) {
123  WebResource wr = wlr.addWebResource(wrp.getLocale(), new WebResource());
124  wr.addFile(wrp, file);
125  }
126  }
127  }
128  }
129 
130  /* Utils */
131 
132  private WebResource webResource(String base, String lang) {
133  if(!Strings.isBlank(base) && !Strings.isBlank(lang)) {
134  WebLocaleResource wlr = resources.get(base);
135  if(wlr != null) {
136  return wlr.getWebResource(lang);
137  }
138  }
139  return null;
140  }
141 
142 }
static void setContent(File file, String value)
Definition: FileUtil.java:274
static boolean isInternal(File file)
Definition: ElContext.java:333
WebResource addWebResource(String lang, WebResource resource)
File getEditableFile(Path folder, IContact contact)
static WebResourceParts editable(IContact contact, String base, String locale)
static WebResourceParts decompose(File file)
void addFile(WebResourceParts wrp, File file)
File getResource(String base, String lang)
void saveContent(Path folder, IContact contact, String base, String lang, String wiki, String html, boolean autoremove)
File getEditable(String base, String lang, String version)
List< String > resourceNamesForLang(String lang)
Set< String > getVersions(String base, String lang)
WebLocaleResource addWebLocaleResource(String base, WebLocaleResource resource)