BrightSide Workbench Full Report + Source Code
FileVersionControl.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.context.vc;
19 
20 import java.io.File;
21 import java.io.IOException;
22 import java.util.Date;
23 import java.util.TreeSet;
24 import org.turro.string.Strings;
25 import org.turro.action.Contacts;
26 import org.turro.elephant.context.IElement;
27 import org.turro.html.HTMLForm;
28 import org.turro.i18n.I_;
29 
34 public class FileVersionControl extends TreeSet<FileProperties> {
35 
36  private IElement iel;
37  private int copies, delay;
38 
40  public FileVersionControl(IElement iel, int copies, int delay) {
41  super(new FPComparator());
42  this.iel = iel;
43  this.copies = copies;
44  this.delay = delay;
45  fillFiles();
46  }
47 
48  public void renderVersions() throws IOException {
49  renderVersions(null, null);
50  }
51 
52  public void renderVersions(String tableAttr, String formAttr) throws IOException {
53  Date today = new Date();
54  HTMLForm hf = new HTMLForm(iel);
55  if(Strings.isBlank(tableAttr)) {
56  hf.startTag("div", "id='control-version'");
57  }
58  if(Strings.isBlank(formAttr)) {
59  hf.startForm("vc" + iel.getId(), "POST", "context");
60  } else {
61  hf.startForm("vc" + iel.getId(), "POST", "context", formAttr);
62  }
63  hf.startTable(Strings.isBlank(tableAttr) ? "" : tableAttr);
64  boolean doCommand = false;
65  for(FileProperties fp : this) {
66  if(fp.isWiki()) {
67  hf.startTableCol("");
68  hf.doTag("span", "class='file-version'");
69  hf.endTableCol();
70  hf.startTableCol("");
71  if(!fp.isLastEntry() &&
72  (iel.getConstructor().isInRole("control-version:admin") ||
73  (iel.getConstructor().isInRole("control-version:editor") &&
75  )) {
76  hf.checkBox(fp.getFullName(), "true", "", (today.getTime() - (1000L * 60L * 60L * 3)) < fp.getModification().getTime()) ;
77  doCommand = true;
78  }
79  hf.endTableCol();
80  hf.startTableCol("");
81  hf.write(I_.byKey(fp.getLang()));
82  hf.endTableCol();
83  hf.startTableCol("");
84  hf.writeDate(fp.getModification(), "short_date_time");
85  hf.endTableCol();
86  hf.startTableCol("");
88  hf.endTableCol();
89  hf.endTableRow();
90  }
91  }
92  if(doCommand) {
93  hf.startTableCol("colspan='10'");
94  hf.command("remove-files", I_.get("Remove selected"), "", "javascript:return confirm('" + I_.get("Ready to delete selection?") + "')");
95  hf.endTableCol();
96  hf.endTableRow();
97  }
98  hf.endTable();
99  hf.endForm();
100  hf.endAllTags();
101  }
102 
103  public void removeFiles() {
104  File files[] = iel.getContext().getFile().listFiles();
105  for(int i = 0; i < files.length; i++) {
106  if(files[i].isFile() && "true".equals(iel.getConstructor().getParameter(files[i].getName()))) {
107  files[i].delete();
108  }
109  }
110  }
111 
112  private void fillFiles() {
113  File files[] = iel.getContext().getFile().listFiles();
114  for(int i = 0; i < files.length; i++) {
115  if(files[i].isFile() && files[i].getName().endsWith(".wiki")) {
116  add(new FileProperties(files[i]));
117  }
118  }
119  String name = "", lang = "";
120  FileProperties lastFp = null;
121  for(FileProperties fp : this) {
122  if(fp.isWiki()) {
123  if(!name.equals(fp.getName()) || !lang.equals(fp.getLang())) {
124  if(lastFp != null) lastFp.setLastEntry(true);
125  }
126  name = fp.getName();
127  lang = fp.getLang();
128  lastFp = fp;
129  }
130  }
131  if(lastFp != null) lastFp.setLastEntry(true);
132  }
133 
134 }
static IContact getContactById(String id)
Definition: Contacts.java:72
void renderVersions(String tableAttr, String formAttr)
FileVersionControl(IElement iel, int copies, int delay)
void writeDate(Date date, String mode)
void startForm(String id, String method, String actionElement)
Definition: HTMLForm.java:39
void command(String command, String value, String style)
Definition: HTMLForm.java:61
void checkBox(String id, String value, String style, boolean checked)
Definition: HTMLForm.java:122
HTMLGenerator doTag(String tag)
HTMLGenerator startTag(String tag)
HTMLGenerator startTableCol(String attributes)
HTMLGenerator startTable(String attributes)
HTMLGenerator write(String value)
Definition: HTMLHelper.java:69
static String byKey(String key)
Definition: I_.java:83
static String get(String msg)
Definition: I_.java:41