BrightSide Workbench Full Report + Source Code
AttachWd.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.entities;
19 
20 import java.io.*;
21 import java.text.ParseException;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import org.turro.string.Strings;
27 import org.turro.attach.db.AttachPU;
28 import org.turro.attach.entity.AttachContent;
29 import org.turro.attach.entity.Attachment;
30 import org.turro.attach.search.AttachHeadlessResults;
31 import org.turro.attach.version.AttachVersionMap;
32 import org.turro.attach.zul.AttachmentUtil;
33 import org.turro.elephant.context.ElephantContext;
34 import org.turro.wd.common.entities.ServerFile;
35 import org.turro.wd.common.entities.WdFormats;
36 
41 public class AttachWd {
42 
43  public static Attachment saveFromFile(File file, String fileName, String fileDate, String path,
44  String user, String comment, boolean lock, boolean validated) {
45  InputStream is = null;
46  try {
47  Attachment attachment = new Attachment();
48  attachment.setFileName(fileName);
49  attachment.setModification(WdFormats.parse(fileDate));
50  attachment.setPath(path);
51  attachment.setOwner(user);
52  attachment.setOnlyOwner(false);
53  attachment.setComment(comment);
54  attachment.setShowKey(null);
55  if(lock) {
56  attachment.setLocked(true);
57  attachment.setLocker(user);
58  }
59  AttachContent ac = new AttachContent();
60  byte[] buffer = new byte[102400];
61  is = new FileInputStream(file);
62  ByteArrayOutputStream baos = new ByteArrayOutputStream();
63  int r;
64  while ((r = is.read(buffer)) != -1) {
65  baos.write(buffer, 0, r);
66  }
67  is.close();
68  ac.setFileContent(baos.toByteArray());
69  attachment.setAttachContent(ac);
71  attachment.setFileSize(ac.getFileContent().length);
72  attachment.setValidated(validated);
73  return new AttachPU().saveObject(attachment);
74  } catch (ParseException ex) {
75  Logger.getLogger(AttachWd.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
76  } catch (IOException ex) {
77  Logger.getLogger(AttachWd.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
78  } finally {
79  try {
80  if(is != null) is.close();
81  } catch (IOException ex) {
82  Logger.getLogger(AttachWd.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
83  }
84  }
85  return null;
86  }
87 
88  public static File convertToFile(String path, String fileName, String user, boolean forEditing) {
89  try {
90  Attachment attachment = queryVersionedAttachment(path, fileName, user);
91  if(forEditing) {
92  if(attachment.isLocked()) return null;
93  attachment.setLocked(true);
94  attachment.setLocker(user);
95  attachment = new AttachPU().saveObject(attachment);
96  }
97  attachment = AttachmentUtil.withContent(attachment);
98  File tempFile = File.createTempFile(AttachWd.class.getName(), null);
99  FileOutputStream outputStream = new FileOutputStream(tempFile);
100  try {
101  ByteArrayInputStream inputStream = new ByteArrayInputStream(attachment.getAttachContent().getFileContent());
102  for(int data = inputStream.read(); data != -1; data = inputStream.read()) {
103  outputStream.write((byte)data);
104  }
105  } finally {
106  outputStream.close();
107  }
108  tempFile.setLastModified(attachment.getModification().getTime());
109  return tempFile;
110  } catch (IOException ex) {
111  Logger.getLogger(AttachWd.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
112  }
113  return null;
114  }
115 
116  public static void writeToFile(Attachment attachment, File file) {
117  try {
118  attachment = AttachmentUtil.withContent(attachment);
119  FileOutputStream outputStream = new FileOutputStream(file);
120  try {
121  ByteArrayInputStream inputStream = new ByteArrayInputStream(attachment.getAttachContent().getFileContent());
122  for(int data = inputStream.read(); data != -1; data = inputStream.read()) {
123  outputStream.write((byte)data);
124  }
125  } finally {
126  outputStream.flush();
127  outputStream.close();
128  }
129  file.setLastModified(attachment.getModification().getTime());
130  } catch (IOException ex) {
131  Logger.getLogger(AttachWd.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
132  }
133  }
134 
135  public static List<String> queryFolders(String path, String user) {
137  results.setAttachValue("*");
138  results.setAttachPath(path + "/_%");
139  results.setCkExactPath(false);
140  return new ArrayList<String>(results.getPathList(path));
141  }
142 
143  public static List<ServerFile> queryAttachments(String path, String user) {
145  results.setAttachValue("*");
146  results.setAttachPath(Strings.isEmpty(path) ? "/" : path);
147  results.setCkExactPath(true);
148  return new ServerFileAdapter(results.getAttachmentList());
149  }
150 
151  public static List<ServerFile> queryVersionedAttachments(String path, String user) {
153  results.setAttachValue("*");
154  results.setAttachPath(Strings.isEmpty(path) ? "/" : path);
155  results.setCkExactPath(true);
157  for(Attachment a : results.getAttachmentList()) {
158  avm.addAttachment(a);
159  }
160  return new ServerFileAdapter(avm.getVersionedAttachments());
161  }
162 
163  public static Attachment queryVersionedAttachment(String path, String fileName, String user) {
165  results.setAttachValue(fileName);
166  results.setAttachPath(Strings.isEmpty(path) ? "/" : path);
167  results.setCkExactPath(true);
169  for(Attachment a : results.getAttachmentList()) {
170  avm.addAttachment(a);
171  }
172  return avm.isEmpty() ? null : avm.get(fileName).first();
173  }
174 
175  public static Object unlockFile(String path, String fileName, String user) {
176  Attachment attachment = queryVersionedAttachment(path, fileName, user);
177  if(attachment != null && attachment.isLocked()) {
178  attachment.setLocked(false);
179  attachment.setLocker(null);
180  attachment.setLockComment(null);
181  attachment.setLockDate(null);
182  attachment.setLockCommit(null);
183  attachment = new AttachPU().saveObject(attachment);
184  }
185  ServerFile sf = new ServerFile();
186  if(attachment != null) {
187  sf.setFileContentType(attachment.getFileContentType());
188  sf.setFileName(attachment.getFileName());
189  sf.setFileSize(attachment.getFileSize());
190  sf.setLocked(attachment.isLocked());
191  sf.setLocker(attachment.getLocker());
192  sf.setModification(attachment.getModification());
193  sf.setOwner(attachment.getOwner());
194  sf.setPath(attachment.getPath());
195  }
196  return sf;
197  }
198 
199  private AttachWd() {
200  }
201 
202 }
void setFileContent(byte[] fileContent)
void setLockCommit(Date lockCommit)
void setShowKey(String showKey)
void setOnlyOwner(boolean onlyOwner)
void setModification(Date modification)
void setFileContentType(String fileContentType)
void setLockComment(String lockComment)
void setValidated(boolean validated)
void setComment(String comment)
void setFileName(String fileName)
void setAttachContent(AttachContent attachContent)
void setLocked(boolean locked)
java.util.Set< String > getPathList(String currPath)
void addAttachment(Attachment attachment)
static Attachment withContent(Attachment attachment)
void setFileContentType(String fileContentType)
Definition: ServerFile.java:37
void setModification(Date modification)
Definition: ServerFile.java:77
static Date parse(String date)
Definition: WdFormats.java:36
static String getContentType(File file)
Definition: WdFormats.java:56
static List< String > queryFolders(String path, String user)
Definition: AttachWd.java:135
static List< ServerFile > queryAttachments(String path, String user)
Definition: AttachWd.java:143
static File convertToFile(String path, String fileName, String user, boolean forEditing)
Definition: AttachWd.java:88
static Attachment queryVersionedAttachment(String path, String fileName, String user)
Definition: AttachWd.java:163
static Attachment saveFromFile(File file, String fileName, String fileDate, String path, String user, String comment, boolean lock, boolean validated)
Definition: AttachWd.java:43
static void writeToFile(Attachment attachment, File file)
Definition: AttachWd.java:116
static Object unlockFile(String path, String fileName, String user)
Definition: AttachWd.java:175
static List< ServerFile > queryVersionedAttachments(String path, String user)
Definition: AttachWd.java:151