BrightSide Workbench Full Report + Source Code
WorkFile.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.wd.files;
19 
20 import java.io.File;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.OutputStream;
24 import java.text.ParseException;
25 import java.util.Date;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import org.apache.pivot.io.FileSerializer;
29 import org.apache.pivot.serialization.BinarySerializer;
30 import org.apache.pivot.serialization.SerializationException;
31 import org.apache.pivot.web.GetQuery;
32 import org.apache.pivot.web.PostQuery;
33 import org.apache.pivot.web.QueryException;
34 import org.jdom.Element;
35 import org.turro.wd.entities.WdFormats;
36 import org.turro.wd.tree.SyncTask;
37 
42 public class WorkFile implements Comparable<WorkFile> {
43 
44  private Directory parent;
45  private String name, serverOwner, serverOwnerName, serverLocker;
46  private Date downModification;
47  private long serverSize;
48  private File file;
49  private boolean serverLocked = false;
50 
51  public Date getDownModification() {
52  return downModification;
53  }
54 
55  public void setDownModification(Date downModification) {
56  this.downModification = downModification;
57  }
58 
59 public File getFile() {
60  return file;
61  }
62 
63  public void setFile(File file) {
64  this.file = file;
65  }
66 
67  public String getName() {
68  return name;
69  }
70 
71  public void setName(String name) {
72  this.name = name;
73  }
74 
75  public Directory getParent() {
76  return parent;
77  }
78 
79  public void setParent(Directory parent) {
80  this.parent = parent;
81  }
82 
83  public boolean isServerLocked() {
84  return serverLocked;
85  }
86 
87  public void setServerLocked(boolean serverLocked) {
88  this.serverLocked = serverLocked;
89  }
90 
91  public String getServerLocker() {
92  return serverLocker;
93  }
94 
95  public void setServerLocker(String serverLocker) {
96  this.serverLocker = serverLocker;
97  }
98 
99  public String getServerOwner() {
100  return serverOwner;
101  }
102 
103  public void setServerOwner(String serverOwner) {
104  this.serverOwner = serverOwner;
105  }
106 
107  public String getServerOwnerName() {
108  return serverOwnerName;
109  }
110 
111  public void setServerOwnerName(String serverOwnerName) {
112  this.serverOwnerName = serverOwnerName;
113  }
114 
115  public long getServerSize() {
116  return serverSize;
117  }
118 
119  public void setServerSize(long serverSize) {
120  this.serverSize = serverSize;
121  }
122 
123  public String getSystemPath() {
124  return parent.getSystemPath() + "/" + getSystemName();
125  }
126 
127  public String getSystemName() {
128  return getName();
129  }
130 
131  public File getSystemFile() {
132  return new File(getSystemPath());
133  }
134 
135  public boolean isExistsOnServer() {
136  return getDownModification() != null;
137  }
138 
139  public boolean isDownloadable() throws ParseException {
140  return (file == null || (getDownModification() != null &&
141  WdFormats.before(downModification, file.lastModified()))) &&
142  parent.isUpdatable();
143  }
144 
145  public boolean isUploadable() throws ParseException {
146  return (file != null && (getDownModification() == null ||
147  WdFormats.after(downModification, file.lastModified()))) &&
148  parent.isUpdatable();
149  }
150 
151  public void readXML(Element root) throws ParseException {
152  if(root.getAttributeValue("name") != null)
153  setName(root.getAttributeValue("name"));
154  }
155 
156  public void writeXML(Element root) {
157  Element el = new Element("wfile");
158  el.setAttribute("name", getName());
159  root.addContent(el);
160  }
161 
162  public boolean checkExisting() {
163  return getSystemFile().exists();
164  }
165 
166  public int compareTo(WorkFile o) {
167  if(getName() == null || o.getName() == null) {
168  return 0;
169  }
170  return getName().compareTo(o.getName());
171  }
172 
173  public void doUpload(String comment, int commitAction) {
174  try {
175  if((commitAction == SyncTask.COMMIT_DO_NOTHING) || !isUploadable()) return;
176  PostQuery postQuery = new PostQuery(
177  parent.getHost(), parent.getPort(),
178  parent.getContext() + "/pservice/upload", false);
179  postQuery.getParameters().put("user", parent.getUser());
180  postQuery.getParameters().put("path", parent.getAbsolutePath());
181  postQuery.getParameters().put("fileName", getName());
182  postQuery.getParameters().put("fileDate", WdFormats.format(file.lastModified()));
183  postQuery.getParameters().put("comment", comment);
184  postQuery.getParameters().put("lock",
185  (((commitAction == SyncTask.COMMIT_KEEP_LOCK) && isServerLocked()) ? "true" : "false"));
186  postQuery.setSerializer(new FileSerializer());
187  postQuery.setValue(file);
188  postQuery.execute();
189  } catch (ParseException ex) {
190  Logger.getLogger(WorkFile.class.getName()).log(Level.SEVERE, null, ex);
191  } catch (QueryException ex) {
192  Logger.getLogger(WorkFile.class.getName()).log(Level.SEVERE, null, ex);
193  }
194  }
195 
196  public void doDownload(int updateAction) {
197  try {
198  if((updateAction == SyncTask.UPDATE_DO_NOTHING) ||
199  (!isDownloadable()) && !(updateAction == SyncTask.UPDATE_UPDATE_FOR_EDITING)) return;
200  GetQuery getQuery = new GetQuery(
201  parent.getHost(), parent.getPort(),
202  parent.getContext() + "/pservice/download", false);
203  getQuery.getParameters().put("user", parent.getUser());
204  getQuery.getParameters().put("forEditing",
205  (updateAction == SyncTask.UPDATE_UPDATE_FOR_EDITING) ? "true" : "false");
206  getQuery.getParameters().put("path", parent.getAbsolutePath());
207  getQuery.getParameters().put("fileName", getName());
208  getQuery.setSerializer(new FileSerializer());
209  File receivedFile = (File) getQuery.execute();
210  new File(parent.getSystemPath()).mkdirs();
211  File saveFile = getSystemFile();
212  OutputStream os = new FileOutputStream(saveFile);
213  ((FileSerializer) getQuery.getSerializer())
214  .writeObject(receivedFile, os);
215  os.close();
216  saveFile.setLastModified(downModification.getTime());
217  } catch (ParseException ex) {
218  Logger.getLogger(WorkFile.class.getName()).log(Level.SEVERE, null, ex);
219  } catch (IOException ex) {
220  Logger.getLogger(WorkFile.class.getName()).log(Level.SEVERE, null, ex);
221  } catch (SerializationException ex) {
222  Logger.getLogger(WorkFile.class.getName()).log(Level.SEVERE, null, ex);
223  } catch (QueryException ex) {
224  Logger.getLogger(WorkFile.class.getName()).log(Level.SEVERE, null, ex);
225  }
226  }
227 
228  public void doUnlock() {
229  try {
230  GetQuery getQuery = new GetQuery(
231  parent.getHost(), parent.getPort(),
232  parent.getContext() + "/pservice/unlock", false);
233  getQuery.getParameters().put("user", parent.getUser());
234  getQuery.getParameters().put("path", parent.getAbsolutePath());
235  getQuery.getParameters().put("fileName", getName());
236  getQuery.setSerializer(new BinarySerializer());
237  getQuery.execute();
238  } catch (QueryException ex) {
239  Logger.getLogger(Directory.class.getName()).log(Level.SEVERE, null, ex);
240  }
241  }
242 }
static String format(Date date)
Definition: WdFormats.java:40
static boolean after(Date date, long time)
Definition: WdFormats.java:52
static boolean before(Date date, long time)
Definition: WdFormats.java:48
void setName(String name)
Definition: WorkFile.java:71
void setFile(File file)
Definition: WorkFile.java:63
void readXML(Element root)
Definition: WorkFile.java:151
void setDownModification(Date downModification)
Definition: WorkFile.java:55
void setServerLocked(boolean serverLocked)
Definition: WorkFile.java:87
void doDownload(int updateAction)
Definition: WorkFile.java:196
void setParent(Directory parent)
Definition: WorkFile.java:79
void setServerLocker(String serverLocker)
Definition: WorkFile.java:95
void setServerOwner(String serverOwner)
Definition: WorkFile.java:103
void doUpload(String comment, int commitAction)
Definition: WorkFile.java:173
void setServerOwnerName(String serverOwnerName)
Definition: WorkFile.java:111
void writeXML(Element root)
Definition: WorkFile.java:156
void setServerSize(long serverSize)
Definition: WorkFile.java:119
int compareTo(WorkFile o)
Definition: WorkFile.java:166
static final int UPDATE_DO_NOTHING
Definition: SyncTask.java:34