001: package org.objectweb.salome_tmf.data;
002:
003: import java.awt.Rectangle;
004: import java.awt.Robot;
005: import java.awt.Toolkit;
006: import java.awt.image.BufferedImage;
007: import java.io.BufferedWriter;
008: import java.io.File;
009: import java.io.FileWriter;
010: import java.net.URL;
011: import java.util.HashMap;
012: import java.util.Iterator;
013: import java.util.Properties;
014: import java.util.Set;
015: import java.util.Vector;
016:
017: import org.objectweb.salome_tmf.api.Api;
018: import org.objectweb.salome_tmf.api.ApiConstants;
019: import org.objectweb.salome_tmf.api.data.FileAttachementWrapper;
020: import org.objectweb.salome_tmf.api.data.UrlAttachementWrapper;
021:
022: abstract public class WithAttachment extends SimpleData {
023:
024: /**
025: * Table contenant les attachements. La clé est le nom de l'attachement et
026: * la valeur l'attachement lui-même.
027: */
028: private HashMap attachmentMap = new HashMap();
029:
030: private boolean attachLoaded = false;
031:
032: protected boolean doClearCache = false;
033:
034: public void clearCache() {
035: if (doClearCache) {
036: if (attachmentMap != null) {
037: attachmentMap.clear();
038: //attachmentMap = null;
039: }
040: attachLoaded = false;
041: }
042: }
043:
044: public void clearAttachCache() {
045: if (doClearCache) {
046: if (attachmentMap != null) {
047: attachmentMap.clear();
048: //attachmentMap = null;
049: }
050: attachLoaded = false;
051: }
052: }
053:
054: void loadAttachment() {
055: if (attachLoaded) {
056: return;
057: }
058: try {
059: loadAttachmentDataFromDB();
060: } catch (Exception e) {
061: e.printStackTrace();
062: }
063: }
064:
065: public WithAttachment(String name, String description) {
066: super (name, description);
067: //attachmentMap = new HashMap();
068: }
069:
070: public HashMap getAttachmentMapFromModel() {
071: loadAttachment();
072:
073: return attachmentMap;
074: }
075:
076: public HashMap getCopyOfAttachmentMapFromModel() {
077: loadAttachment();
078:
079: HashMap copyAttachMap = new HashMap();
080: Set keysSet = attachmentMap.keySet();
081: for (Iterator iter = keysSet.iterator(); iter.hasNext();) {
082: Object elem = iter.next();
083: copyAttachMap.put(elem, attachmentMap.get(elem));
084: }
085: return copyAttachMap;
086: }
087:
088: protected void clearAttachInModel() {
089: if (attachmentMap != null) {
090: attachmentMap.clear();
091: //attachmentMap = null;
092: }
093: }
094:
095: public void addAttchmentInModel(String fileName, String value) {
096: try {
097: FileAttachment attach = makeFileAttachment(fileName, value);
098: addAttachementInModel(attach);
099: } catch (Exception e) {
100:
101: }
102: }
103:
104: public FileAttachment takeScreenShot() throws Exception {
105: String fileName = "screenshot.jpg";
106: Properties sys = System.getProperties();
107: String tempDir = sys.getProperty("java.io.tmpdir");
108: String fs = sys.getProperty("file.separator");
109: tempDir = tempDir + fs + ApiConstants.PATH_TO_ADD_TO_TEMP;
110: File fDir = new File(tempDir);
111: if (!fDir.exists()) {
112: fDir.mkdirs();
113: }
114: int num = 0;
115: String pFileName = tempDir + fs + fileName;
116: File file = new File(pFileName);
117: while (file.exists()
118: || containsAttachmentInModel(file.getName())) {
119: pFileName = tempDir + fs + num + fileName;
120: file = new File(pFileName);
121: num++;
122: }
123: file.createNewFile();
124:
125: Robot robot = new Robot();
126: robot.setAutoDelay(0);
127: robot.setAutoWaitForIdle(false);
128:
129: Rectangle captureSize = new Rectangle(Toolkit
130: .getDefaultToolkit().getScreenSize());
131: BufferedImage bufferedImage = robot
132: .createScreenCapture(captureSize);
133:
134: javax.imageio.ImageIO.write(bufferedImage, "jpg", file);
135: return makeFileAttachment(file);
136: }
137:
138: public FileAttachment makeFileAttachment(String fileName,
139: String value) throws Exception {
140: Properties sys = System.getProperties();
141: String tempDir = sys.getProperty("java.io.tmpdir");
142: String fs = sys.getProperty("file.separator");
143: tempDir = tempDir + fs + ApiConstants.PATH_TO_ADD_TO_TEMP;
144: int num = 0;
145: String pFileName = tempDir + fs + fileName;
146: File file = new File(pFileName);
147: while (file.exists()
148: || containsAttachmentInModel(file.getName())) {
149: pFileName = tempDir + fs + num + fileName;
150: file = new File(pFileName);
151: num++;
152: //errorFile.delete();
153: }
154: file.createNewFile();
155: FileWriter fw = new FileWriter(pFileName, true);
156: // le BufferedWriter output auquel on donne comme argument le FileWriter fw cree juste au dessus
157: BufferedWriter output = new BufferedWriter(fw);
158: //on marque dans le fichier ou plutot dans le BufferedWriter qui sert comme un tampon(stream)
159: output.write(value);
160: //on peut utiliser plusieurs fois methode write
161: output.flush();
162: //ensuite flush envoie dans le fichier, ne pas oublier cette methode pour le BufferedWriter
163: output.close();
164: //et on le ferme
165: return makeFileAttachment(file);
166: }
167:
168: public void addAttchmentInModel(File file) {
169: FileAttachment pFileAttachment = makeFileAttachment(file);
170: /*if (attachmentMap == null)
171: attachmentMap = new HashMap();
172:
173: attachmentMap.put(pFileAttachment.getNameFromModel(), pFileAttachment);*/
174: addAttachementInModel(pFileAttachment);
175: }
176:
177: public FileAttachment makeFileAttachment(File file) {
178: FileAttachment pFileAttachment = new FileAttachment(file, "");
179: return pFileAttachment;
180: }
181:
182: public void addAttchmentInModel(URL pUrl) {
183: UrlAttachment urlAttachment = makeUrlAttchment(pUrl);
184:
185: /*if (attachmentMap == null)
186: attachmentMap = new HashMap();
187:
188: attachmentMap.put(urlAttachment.getNameFromModel(), urlAttachment);
189: */
190: addAttachementInModel(urlAttachment);
191: }
192:
193: public UrlAttachment makeUrlAttchment(URL pUrl) {
194: UrlAttachment urlAttachment = new UrlAttachment(
195: pUrl.toString(), "");
196: urlAttachment.setUrl(pUrl);
197: return urlAttachment;
198: }
199:
200: public void addAttachementInDBAndModel(Attachment attach)
201: throws Exception {
202: addAttachementInDB(attach);
203: addAttachementInModel(attach);
204: }
205:
206: abstract public void addAttachementInDB(Attachment attach)
207: throws Exception;
208:
209: public void addAttachementInModel(Attachment attach) {
210: if (attachmentMap == null) {
211: attachmentMap = new HashMap();
212: }
213: attachmentMap.put(attach.getNameFromModel(), attach);
214: //String uniqueName = getUniqueName(attach.getNameFromModel());
215: //attachmentMap.put(uniqueName, attach);
216: }
217:
218: abstract public void deleteAttachementInDBAndModel(Attachment attach)
219: throws Exception;
220:
221: abstract protected void deleteAttachementInDB(int idAttach)
222: throws Exception;
223:
224: public void deleteAttachmentInModel(Attachment attach) {
225: attachmentMap.remove(attach.getNameFromModel());
226: if (attachmentMap.containsValue(attach)) {
227: Set keysSet = attachmentMap.keySet();
228: for (Iterator iter = keysSet.iterator(); iter.hasNext();) {
229: Object attachName = iter.next();
230: Attachment at = (Attachment) attachmentMap
231: .get(attachName);
232: if (attach.equals(at)) {
233: attachmentMap.remove(attachName);
234: return;
235: }
236: }
237: } else {
238: attachmentMap.remove(attach.getNameFromModel());
239: }
240: }
241:
242: /*
243: public void deleteAttachmentInModel(String attachName) {
244: attachmentMap.remove(attachName);
245: }
246: */
247:
248: public Attachment getAttachmentFromModel(String attachName) {
249: loadAttachment();
250: return (Attachment) attachmentMap.get(attachName);
251:
252: }
253:
254: public boolean containsAttachmentInModel(String attachName) {
255: loadAttachment();
256: return attachmentMap.containsKey(attachName);
257: }
258:
259: public boolean containsAttachmentInModel(Attachment attach) {
260: loadAttachment();
261: return attachmentMap.containsValue(attach);
262: }
263:
264: public void setAttachmentMapInModel(HashMap map) {
265: //Util.log("[WithAttachemnt->SetAttachementMap]");
266: attachmentMap = map;
267: if (attachmentMap != null) {
268: attachLoaded = true;
269: } else {
270: attachLoaded = false;
271: }
272: }
273:
274: abstract public Vector getAttachFilesFromDB() throws Exception;
275:
276: abstract public Vector getAttachUrlsFromDB() throws Exception;
277:
278: public void reloadAttachmentDataFromDB(boolean force)
279: throws Exception {
280: clearAttachInModel();
281: if (!DataLoader.isDynamic || force) {
282: int transNuber = -1;
283: try {
284: transNuber = Api.beginTransaction(0,
285: ApiConstants.LOADING);
286: loadAttachmentDataFromDB();
287: Api.commitTrans(transNuber);
288: } catch (Exception e) {
289: Api.forceRollBackTrans(transNuber);
290: throw e;
291: }
292: }
293: }
294:
295: void loadAttachmentDataFromDB() throws Exception {
296: Vector fileNameAttachVector = getAttachFilesFromDB();
297: Vector urlAttachVector = getAttachUrlsFromDB();
298: if (attachmentMap == null) {
299: attachmentMap = new HashMap();
300: }
301: for (int i = 0; i < fileNameAttachVector.size(); i++) {
302: FileAttachment fileAttach = new FileAttachment(
303: (FileAttachementWrapper) fileNameAttachVector
304: .get(i));
305: addAttachementInModel(fileAttach);
306: }
307:
308: for (int i = 0; i < urlAttachVector.size(); i++) {
309: UrlAttachment urlAttach = new UrlAttachment(
310: (UrlAttachementWrapper) urlAttachVector.get(i));
311: addAttachementInModel(urlAttach);
312: }
313: attachLoaded = true;
314: }
315:
316: public void updateAttachement(HashMap oldAttachMap)
317: throws Exception {
318: /* Les attachement */
319: //loadAttachment();
320: HashMap newAttachMap = getAttachmentMapFromModel();
321:
322: Set keysSet = newAttachMap.keySet();
323: for (Iterator iter = keysSet.iterator(); iter.hasNext();) {
324: Object attachName = iter.next();
325: Attachment at = (Attachment) oldAttachMap.get(attachName);
326: Attachment attach = (Attachment) newAttachMap
327: .get(attachName);
328: if (at == null) {
329: /* New */
330: addAttachementInDB(attach);
331: } else {
332: /* Update */
333: String oldDesc = at.getDescriptionFromModel();
334: String newDesc = attach.getDescriptionFromModel();
335: if (!oldDesc.equals(newDesc)) {
336: attach.updateDescriptionInDB(attach
337: .getDescriptionFromModel());
338: }
339: }
340: }
341:
342: keysSet = oldAttachMap.keySet();
343: for (Iterator iter = keysSet.iterator(); iter.hasNext();) {
344: Object attachName = iter.next();
345: Attachment at = (Attachment) newAttachMap.get(attachName);
346: Attachment attach = (Attachment) oldAttachMap
347: .get(attachName);
348: if (at == null) {
349: /* remove attach */
350: deleteAttachementInDBAndModel(attach);
351: }
352: }
353: }
354: /*
355: String getUniqueName(String name){
356: if (attachmentMap == null){
357: return name;
358: }
359: boolean trouve = false;
360: int copy = 0;
361: String tmpName = name;
362: while (!trouve){
363: Object o = attachmentMap.get(tmpName);
364: if (o != null){
365: copy++;
366: tmpName = name + copy;
367: }else {
368: trouve = true;
369: }
370: }
371: return tmpName;
372: }*/
373: }
|