142 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.erp.core.edm;
|
|
|
15 |
|
|
|
16 |
import java.io.BufferedInputStream;
|
|
|
17 |
import java.io.File;
|
|
|
18 |
import java.io.FileInputStream;
|
|
|
19 |
import java.io.IOException;
|
|
|
20 |
import java.sql.SQLException;
|
|
|
21 |
import java.util.Collection;
|
|
|
22 |
import java.util.List;
|
|
|
23 |
|
|
|
24 |
import javax.swing.JOptionPane;
|
|
|
25 |
|
|
|
26 |
import eu.medsea.mimeutil.MimeType;
|
|
|
27 |
import eu.medsea.mimeutil.MimeUtil;
|
149 |
ilm |
28 |
import org.openconcerto.erp.config.ComptaPropsConfiguration;
|
|
|
29 |
import org.openconcerto.erp.generationDoc.DocumentLocalStorageManager;
|
|
|
30 |
import org.openconcerto.erp.storage.StorageEngine;
|
|
|
31 |
import org.openconcerto.erp.storage.StorageEngines;
|
|
|
32 |
import org.openconcerto.sql.model.SQLRow;
|
|
|
33 |
import org.openconcerto.sql.model.SQLRowAccessor;
|
|
|
34 |
import org.openconcerto.sql.model.SQLRowValues;
|
|
|
35 |
import org.openconcerto.sql.model.SQLTable;
|
|
|
36 |
import org.openconcerto.utils.ExceptionHandler;
|
|
|
37 |
import org.openconcerto.utils.FileUtils;
|
|
|
38 |
import org.openconcerto.utils.sync.SyncClient;
|
142 |
ilm |
39 |
|
|
|
40 |
public class AttachmentUtils {
|
|
|
41 |
|
149 |
ilm |
42 |
public void uploadFile(File inFile, SQLRowAccessor rowSource, int idParent) {
|
142 |
ilm |
43 |
try {
|
|
|
44 |
|
|
|
45 |
// Création de la row attachment
|
|
|
46 |
SQLRowValues rowValsAttachment = new SQLRowValues(rowSource.getTable().getTable("ATTACHMENT"));
|
|
|
47 |
rowValsAttachment.put("SOURCE_TABLE", rowSource.getTable().getName());
|
|
|
48 |
rowValsAttachment.put("SOURCE_ID", rowSource.getID());
|
149 |
ilm |
49 |
rowValsAttachment.put("ID_PARENT", idParent);
|
142 |
ilm |
50 |
SQLRow rowAttachment = rowValsAttachment.insert();
|
|
|
51 |
int id = rowAttachment.getID();
|
|
|
52 |
|
149 |
ilm |
53 |
final String folderId = String.valueOf((id / 1000) * 1000);
|
|
|
54 |
String subDir = "EDM" + File.separator + folderId;
|
142 |
ilm |
55 |
String fileNameID = String.valueOf(id);
|
|
|
56 |
String ext = "";
|
|
|
57 |
|
|
|
58 |
int i = inFile.getName().lastIndexOf('.');
|
|
|
59 |
if (i > 0) {
|
|
|
60 |
ext = inFile.getName().substring(i + 1);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
final String fileWithIDNAme = fileNameID + "." + ext;
|
|
|
64 |
|
|
|
65 |
final ComptaPropsConfiguration config = ComptaPropsConfiguration.getInstanceCompta();
|
|
|
66 |
boolean isOnCloud = config.isOnCloud();
|
|
|
67 |
|
|
|
68 |
if (isOnCloud) {
|
|
|
69 |
|
|
|
70 |
String remotePath = subDir;
|
|
|
71 |
remotePath = remotePath.replace('\\', '/');
|
|
|
72 |
List<StorageEngine> engines = StorageEngines.getInstance().getActiveEngines();
|
|
|
73 |
for (StorageEngine storageEngine : engines) {
|
|
|
74 |
if (storageEngine.isConfigured() && storageEngine.allowAutoStorage()) {
|
|
|
75 |
final String path = remotePath;
|
149 |
ilm |
76 |
|
|
|
77 |
try (FileInputStream in = new FileInputStream(inFile)) {
|
142 |
ilm |
78 |
storageEngine.connect();
|
149 |
ilm |
79 |
final BufferedInputStream inStream = new BufferedInputStream(in);
|
142 |
ilm |
80 |
storageEngine.store(inStream, path, fileWithIDNAme, true);
|
|
|
81 |
inStream.close();
|
|
|
82 |
storageEngine.disconnect();
|
|
|
83 |
} catch (IOException e) {
|
|
|
84 |
ExceptionHandler.handle("Impossible de sauvegarder le fichier " + inFile.getAbsolutePath() + " vers " + path + "(" + storageEngine + ")", e);
|
|
|
85 |
}
|
|
|
86 |
// if (storageEngine instanceof CloudStorageEngine) {
|
|
|
87 |
// try {
|
|
|
88 |
// storageEngine.connect();
|
|
|
89 |
// final BufferedInputStream inStream = new BufferedInputStream(new
|
|
|
90 |
// FileInputStream(generatedFile));
|
|
|
91 |
// storageEngine.store(inStream, path, generatedFile.getName(), true);
|
|
|
92 |
// inStream.close();
|
|
|
93 |
// storageEngine.disconnect();
|
|
|
94 |
// } catch (IOException e) {
|
|
|
95 |
// ExceptionHandler.handle("Impossible de sauvegarder le fichier généré " +
|
|
|
96 |
// generatedFile.getAbsolutePath() + " vers " + path + "(" + storageEngine +
|
|
|
97 |
// ")", e);
|
|
|
98 |
// }
|
|
|
99 |
// }
|
|
|
100 |
}
|
|
|
101 |
}
|
|
|
102 |
} else {
|
|
|
103 |
// Upload File
|
|
|
104 |
|
|
|
105 |
// Get file out
|
|
|
106 |
File dirRoot = DocumentLocalStorageManager.getInstance().getDocumentOutputDirectory(AttachmentSQLElement.DIRECTORY_PREFS);
|
|
|
107 |
File storagePathFile = new File(dirRoot, subDir);
|
|
|
108 |
storagePathFile.mkdirs();
|
|
|
109 |
// TODO CHECK IF FILE EXISTS
|
|
|
110 |
FileUtils.copyFile(inFile, new File(storagePathFile, fileWithIDNAme));
|
|
|
111 |
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
// Update rowAttachment
|
|
|
115 |
rowValsAttachment = rowAttachment.createEmptyUpdateRow();
|
|
|
116 |
|
|
|
117 |
// Default is without extension
|
|
|
118 |
String fileName = inFile.getName();
|
|
|
119 |
String name = fileName;
|
|
|
120 |
int index = name.lastIndexOf('.');
|
|
|
121 |
if (index > 0) {
|
|
|
122 |
name = name.substring(0, index);
|
|
|
123 |
}
|
|
|
124 |
rowValsAttachment.put("NAME", name);
|
|
|
125 |
rowValsAttachment.put("SOURCE_TABLE", rowSource.getTable().getName());
|
|
|
126 |
rowValsAttachment.put("SOURCE_ID", rowSource.getID());
|
|
|
127 |
Collection<MimeType> mimeTypes = MimeUtil.getMimeTypes(inFile);
|
|
|
128 |
if (mimeTypes != null && !mimeTypes.isEmpty()) {
|
|
|
129 |
final MimeType mimeType = (MimeType) mimeTypes.toArray()[0];
|
|
|
130 |
rowValsAttachment.put("MIMETYPE", mimeType.getMediaType() + "/" + mimeType.getSubType());
|
|
|
131 |
} else {
|
|
|
132 |
rowValsAttachment.put("MIMETYPE", "application/octet-stream");
|
|
|
133 |
}
|
|
|
134 |
rowValsAttachment.put("FILENAME", fileName);
|
|
|
135 |
rowValsAttachment.put("FILESIZE", inFile.length());
|
|
|
136 |
rowValsAttachment.put("STORAGE_PATH", subDir);
|
|
|
137 |
rowValsAttachment.put("STORAGE_FILENAME", fileWithIDNAme);
|
|
|
138 |
// TODO THUMBNAIL
|
|
|
139 |
// rowVals.put("THUMBNAIL", );
|
|
|
140 |
// rowVals.put("THUMBNAIL_WIDTH", );
|
|
|
141 |
// rowVals.put("THUMBNAIL_HEIGHT", );
|
|
|
142 |
|
|
|
143 |
// needed for update count
|
|
|
144 |
|
|
|
145 |
rowValsAttachment.commit();
|
149 |
ilm |
146 |
final Attachment a = new Attachment(rowValsAttachment);
|
|
|
147 |
updateAttachmentsCountFromAttachment(a);
|
|
|
148 |
} catch (Exception e) {
|
|
|
149 |
e.printStackTrace();
|
142 |
ilm |
150 |
}
|
|
|
151 |
}
|
|
|
152 |
|
149 |
ilm |
153 |
public File getFile(Attachment attachment) {
|
142 |
ilm |
154 |
|
|
|
155 |
final ComptaPropsConfiguration config = ComptaPropsConfiguration.getInstanceCompta();
|
|
|
156 |
boolean isOnCloud = config.isOnCloud();
|
|
|
157 |
|
149 |
ilm |
158 |
String subDir = attachment.getStoragePath();
|
|
|
159 |
String fileName = attachment.getStorageFileName();
|
142 |
ilm |
160 |
|
|
|
161 |
String remotePath = config.getSocieteID() + File.separator + subDir;
|
|
|
162 |
|
|
|
163 |
File fTemp;
|
|
|
164 |
try {
|
|
|
165 |
fTemp = File.createTempFile("edm_", "oc");
|
|
|
166 |
} catch (IOException e) {
|
|
|
167 |
ExceptionHandler.handle("Impossible de créer le fichier temporaire de réception", e);
|
|
|
168 |
return null;
|
|
|
169 |
}
|
|
|
170 |
File f = new File(fTemp.getParent(), fTemp.getName() + "-dir");
|
|
|
171 |
f.mkdirs();
|
|
|
172 |
fTemp.delete();
|
|
|
173 |
|
|
|
174 |
if (isOnCloud) {
|
|
|
175 |
remotePath = remotePath.replace('\\', '/');
|
|
|
176 |
final SyncClient client = new SyncClient("https://" + config.getStorageServer());
|
|
|
177 |
|
|
|
178 |
client.setVerifyHost(false);
|
|
|
179 |
|
|
|
180 |
try {
|
|
|
181 |
client.retrieveFile(f, remotePath, fileName, config.getToken());
|
|
|
182 |
} catch (Exception e) {
|
|
|
183 |
ExceptionHandler.handle("Impossible de récupérer le fichier depuis le cloud", e);
|
|
|
184 |
return null;
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
} else {
|
|
|
188 |
|
|
|
189 |
// Get file out
|
|
|
190 |
File dirRoot = DocumentLocalStorageManager.getInstance().getDocumentOutputDirectory(AttachmentSQLElement.DIRECTORY_PREFS);
|
|
|
191 |
File storagePathFile = new File(dirRoot, subDir);
|
|
|
192 |
File fileIn = new File(storagePathFile, fileName);
|
|
|
193 |
if (fileIn.exists()) {
|
|
|
194 |
final File outFile = new File(f, fileName);
|
|
|
195 |
try {
|
|
|
196 |
FileUtils.copyFile(fileIn, outFile);
|
|
|
197 |
} catch (IOException e) {
|
|
|
198 |
ExceptionHandler.handle("Impossible de copier le fichier vers le fichier temporaire de réception", e);
|
|
|
199 |
return null;
|
|
|
200 |
}
|
|
|
201 |
} else {
|
|
|
202 |
JOptionPane.showMessageDialog(null, "Le fichier n'existe pas sur le serveur!", "Erreur fichier", JOptionPane.ERROR_MESSAGE);
|
|
|
203 |
}
|
|
|
204 |
}
|
|
|
205 |
final File outFile = new File(f, fileName);
|
|
|
206 |
outFile.setReadOnly();
|
|
|
207 |
return outFile;
|
|
|
208 |
|
|
|
209 |
}
|
|
|
210 |
|
149 |
ilm |
211 |
public void deleteFile(Attachment rowAttachment) throws SQLException, IllegalStateException {
|
142 |
ilm |
212 |
|
|
|
213 |
final ComptaPropsConfiguration config = ComptaPropsConfiguration.getInstanceCompta();
|
149 |
ilm |
214 |
if (!rowAttachment.isFolder()) {
|
|
|
215 |
boolean isOnCloud = config.isOnCloud();
|
|
|
216 |
// Delete File
|
|
|
217 |
String subDir = rowAttachment.getStoragePath();
|
|
|
218 |
String fileName = rowAttachment.getStorageFileName();
|
142 |
ilm |
219 |
|
149 |
ilm |
220 |
String remotePath = config.getSocieteID() + File.separator + subDir;
|
|
|
221 |
if (isOnCloud) {
|
|
|
222 |
remotePath = remotePath.replace('\\', '/');
|
|
|
223 |
// final SyncClient client = new SyncClient("https://" + config.getStorageServer());
|
|
|
224 |
//
|
|
|
225 |
// client.setVerifyHost(false);
|
142 |
ilm |
226 |
|
149 |
ilm |
227 |
// TODO DELETE FILE ON CLOUD OR RENAME?
|
|
|
228 |
// client.retrieveFile(f, remotePath, fileName, config.getToken());
|
|
|
229 |
} else {
|
142 |
ilm |
230 |
|
149 |
ilm |
231 |
File dirRoot = DocumentLocalStorageManager.getInstance().getDocumentOutputDirectory(AttachmentSQLElement.DIRECTORY_PREFS);
|
|
|
232 |
File storagePathFile = new File(dirRoot, subDir);
|
|
|
233 |
File f = new File(storagePathFile, fileName);
|
|
|
234 |
if (f.exists()) {
|
|
|
235 |
if (!f.delete()) {
|
|
|
236 |
throw new IllegalStateException("Une erreur est survenue lors de la suppression du fichier");
|
|
|
237 |
}
|
142 |
ilm |
238 |
}
|
|
|
239 |
}
|
|
|
240 |
}
|
|
|
241 |
// Delete Row
|
149 |
ilm |
242 |
config.getDirectory().getElement(AttachmentSQLElement.class).archive(rowAttachment.getId());
|
142 |
ilm |
243 |
updateAttachmentsCountFromAttachment(rowAttachment);
|
|
|
244 |
}
|
|
|
245 |
|
149 |
ilm |
246 |
private void updateAttachmentsCountFromAttachment(Attachment rowAttachment) {
|
|
|
247 |
final ComptaPropsConfiguration config = ComptaPropsConfiguration.getInstanceCompta();
|
|
|
248 |
final SQLTable attachmentTable = config.getDirectory().getElement(AttachmentSQLElement.class).getTable();
|
|
|
249 |
final SQLTable table = attachmentTable.getTable(rowAttachment.getSourceTable());
|
|
|
250 |
final SQLRow source = table.getRow(rowAttachment.getSourceId());
|
142 |
ilm |
251 |
updateAttachmentsCountFromSource(source);
|
|
|
252 |
}
|
|
|
253 |
|
149 |
ilm |
254 |
private void updateAttachmentsCountFromSource(SQLRow rowSource) {
|
|
|
255 |
final SQLTable tableSource = rowSource.getTable();
|
|
|
256 |
final SQLTable tableAtt = rowSource.getTable().getTable("ATTACHMENT");
|
142 |
ilm |
257 |
|
|
|
258 |
String req = "UPDATE " + tableSource.getSQLName().quote() + " SET " + tableSource.getField("ATTACHMENTS").getQuotedName() + "=(SELECT COUNT(*) FROM " + tableAtt.getSQLName().quote();
|
|
|
259 |
req += " WHERE " + tableAtt.getArchiveField().getQuotedName() + "=0 AND " + tableAtt.getField("SOURCE_TABLE").getQuotedName() + "='" + tableSource.getName() + "'";
|
|
|
260 |
req += " AND " + tableAtt.getField("SOURCE_ID").getQuotedName() + "=" + rowSource.getID() + ") WHERE " + tableSource.getKey().getQuotedName() + "=" + rowSource.getID();
|
|
|
261 |
|
|
|
262 |
tableSource.getDBSystemRoot().getDataSource().execute(req);
|
|
|
263 |
}
|
|
|
264 |
|
149 |
ilm |
265 |
public static void rename(Attachment rowAttachment, String newName) {
|
|
|
266 |
rowAttachment.setName(newName);
|
|
|
267 |
final ComptaPropsConfiguration config = ComptaPropsConfiguration.getInstanceCompta();
|
|
|
268 |
final SQLTable attachmentTable = config.getDirectory().getElement(AttachmentSQLElement.class).getTable();
|
|
|
269 |
final String req = "UPDATE " + attachmentTable.getSQLName().quote() + " SET " + attachmentTable.getField("NAME").getQuotedName() + "=" + attachmentTable.getBase().quoteString(newName)
|
|
|
270 |
+ " WHERE " + attachmentTable.getKey().getQuotedName() + "=" + rowAttachment.getId();
|
|
|
271 |
attachmentTable.getDBSystemRoot().getDataSource().execute(req);
|
|
|
272 |
}
|
|
|
273 |
|
|
|
274 |
public void createFolder(String folderName, SQLRowAccessor rowSource, int idParent) throws SQLException {
|
|
|
275 |
final SQLRowValues rowValsAttachment = new SQLRowValues(rowSource.getTable().getTable("ATTACHMENT"));
|
|
|
276 |
rowValsAttachment.put("SOURCE_TABLE", rowSource.getTable().getName());
|
|
|
277 |
rowValsAttachment.put("SOURCE_ID", rowSource.getID());
|
|
|
278 |
rowValsAttachment.put("ID_PARENT", idParent);
|
|
|
279 |
rowValsAttachment.put("NAME", folderName);
|
|
|
280 |
rowValsAttachment.put("SOURCE_TABLE", rowSource.getTable().getName());
|
|
|
281 |
rowValsAttachment.put("SOURCE_ID", rowSource.getID());
|
|
|
282 |
rowValsAttachment.put("MIMETYPE", Attachment.MIMETYPE_FOLDER);
|
|
|
283 |
rowValsAttachment.put("FILENAME", "");
|
|
|
284 |
rowValsAttachment.put("FILESIZE", 0);
|
|
|
285 |
rowValsAttachment.put("STORAGE_PATH", "");
|
|
|
286 |
rowValsAttachment.put("STORAGE_FILENAME", "");
|
|
|
287 |
rowValsAttachment.commit();
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
public void move(Attachment a, Attachment folder) {
|
|
|
291 |
if (!folder.isFolder()) {
|
|
|
292 |
throw new IllegalArgumentException(folder + " is not a folder");
|
|
|
293 |
}
|
|
|
294 |
move(a, folder.getId());
|
|
|
295 |
}
|
|
|
296 |
|
|
|
297 |
public void move(Attachment a, int folderId) {
|
|
|
298 |
final ComptaPropsConfiguration config = ComptaPropsConfiguration.getInstanceCompta();
|
|
|
299 |
final SQLTable attachmentTable = config.getDirectory().getElement(AttachmentSQLElement.class).getTable();
|
|
|
300 |
final String req = "UPDATE " + attachmentTable.getSQLName().quote() + " SET " + attachmentTable.getField("ID_PARENT").getQuotedName() + "=" + folderId + " WHERE "
|
|
|
301 |
+ attachmentTable.getKey().getQuotedName() + "=" + a.getId();
|
|
|
302 |
attachmentTable.getDBSystemRoot().getDataSource().execute(req);
|
|
|
303 |
}
|
|
|
304 |
|
142 |
ilm |
305 |
}
|