BrightSide Workbench Full Report + Source Code
FileBean.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.elephant.impl.util;
19 
20 import java.io.BufferedReader;
21 import java.io.File;
22 import java.io.IOException;
23 import org.turro.elephant.context.ElephantContext;
24 import org.turro.elephant.context.IConstructor;
25 import org.turro.elephant.web.ElContext;
26 import org.turro.elephant.web.ElContextMap;
27 
32 public class FileBean {
33  private IConstructor constructor;
34  private String context, file, action, lastWiki = null;
35  private int localeCount = 0;
36  private String[] defaultLocales;
37 
39  public FileBean() {
40  }
41 
43  return constructor;
44  }
45 
46  public String getRealContext() {
47  return FileUtil.getRealContext(context);
48  }
49 
50  public String getRepositoryPath() {
51  return context + "/_internal/repository";
52  }
53 
54  public String[] getAllowedLocales() {
55  if(defaultLocales == null) {
56  ElContext currContext = ElContextMap.getContext(context);
57  defaultLocales = currContext.getDefaultLocales().toArray(new String[0]);
58  }
59  return defaultLocales;
60  }
61 
62  public String getSelectedLocale() {
63  if(localeCount > defaultLocales.length) localeCount = 0;
64  return file.startsWith(FileUtil.crudeName(file) + defaultLocales[localeCount++] + ".")
65  ? "selected" : "";
66  }
67 
68  public String getLastWikiString() {
69  if(file == null || file.length() == 0) return null;
70  if(lastWiki == null) {
71  File folder = new File(ElephantContext.getRealPath(context));
72  File files[] = folder.listFiles();
73  String filePrefix = FileUtil.crudeName(file),
74  fileName, lang = FileUtil.getLang(file);
75  for(int i = 0; i < files.length; i++) {
76  fileName = files[i].getName();
77  if(fileName.endsWith(lang + ".wiki") && fileName.startsWith(filePrefix)) {
78  lastWiki = (lastWiki == null || fileName.compareTo(lastWiki) > 0) ? fileName : lastWiki;
79  }
80  }
81  }
82  return lastWiki;
83  }
84 
85  public String getWiki() throws IOException {
86  File folder = new File(ElephantContext.getRealPath(context));
87  if(!folder.exists()) folder.mkdirs();
88  if("new".equals(action)) return "";
89  File edit = new File(ElephantContext.getRealPath(context + "/" + file));
90  if(edit.exists()) {
91  String lastWiki = getLastWikiString();
92  File content = null;
93  if(lastWiki != null) {
94  content = new File(ElephantContext.getRealPath(context + "/" + lastWiki));
95  }
96  else {
97  content = edit;
98  }
99  StringBuilder sb = new StringBuilder();
100  try (BufferedReader br = FileUtil.getBufferedFile(content)) {
101  String s;
102  while((s = br.readLine()) != null) {
103  sb.append(s + "\n");
104  }
105  }
106  return sb.toString();
107  }
108  return "";
109  }
110 
111  public void setConstructor(IConstructor constructor) {
112  this.constructor = constructor;
113  }
114 
115  public String getContext() {
116  return context;
117  }
118 
119  public void setContext(String context) {
120  this.context = context;
121  }
122 
123  public String getFile() {
124  return file;
125  }
126 
127  public void setFile(String file) {
128  this.file = file;
129  }
130 
131  public String getAction() {
132  return action;
133  }
134 
135  public void setAction(String action) {
136  this.action = action;
137  }
138 
139 }
void setConstructor(IConstructor constructor)
Definition: FileBean.java:111
static String getLang(String file)
Definition: FileUtil.java:228
static String getRealContext(String context)
Definition: FileUtil.java:237
static String crudeName(String file)
Definition: FileUtil.java:207
static BufferedReader getBufferedFile(String folder, String file)
Definition: FileUtil.java:146
static ElContext getContext(IConstructor constructor)