Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/flexstor/remote/script/ScriptableFrame.java


1   /*
2    * ScriptableFrame.java
3    *
4    * Copyright $Date: 2003/08/11 02:22:36 $ FLEXSTOR.net Inc.
5    *
6    * This work is licensed for use and distribution under license terms found at
7    * http://www.flexstor.org/license.html
8    *
9    */
10  
11  package com.flexstor.remote.script;
12  
13  import java.util.Enumeration;
14  import java.util.Hashtable;
15  import java.util.Vector;
16  
17  import com.flexstor.common.data.ejb.disguiserecord.DisguiseAssetRecordData;
18  import com.flexstor.common.importprocessor.ImportData;
19  import com.flexstor.common.util.Diagnostic;
20  
21  public class ScriptableFrame
22  extends java.awt.Frame
23  {
24     ImportData impData = null;
25     ScriptService service = null;
26     Hashtable assetRecords =  null;
27     int index = -1; // index of current DisguiseAssetRecordData
28     //String[] assetKeys = null; // used with index to get an asset
29     Vector assetKeys = null; // holds a list of filenames, used with index to get an asset
30     DisguiseAssetRecordData currentAsset = null; // The current 'in' Asset
31     Hashtable currentData = null; // Key value pairs of data from the current asset
32     DisguiseAssetRecordData currentOut = null; // The current 'out' Asset which is a child of the current Asset
33     Hashtable currentOutData = null; // key value pairs sent in by an Applescript.
34     
35     public ScriptableFrame(Hashtable Records, ScriptService svc, String scriptName)
36     {
37        super(scriptName);
38        setName(scriptName);
39        setSize(40,20);
40        assetRecords = Records;
41        if(assetRecords != null)
42        { 
43           //svc.displayHash(assetRecords);
44           assetKeys = new Vector(assetRecords.size());
45           Enumeration enum = assetRecords.keys();
46           
47           for (int x = 0; x < assetRecords.size(); x++) 
48           {
49              assetKeys.addElement((String)enum.nextElement());
50           }
51           
52        }
53        service = svc;
54        setVisible(true);
55    }
56     
57     /**
58     * getNumberOfAssets
59     * @return The number of assets in the list
60     **/
61     public int GetNumberOfAssets()
62     {
63          if(assetRecords != null)
64              return assetRecords.size();
65          else
66              return 0;
67     }
68    
69     
70    /**
71     * Sets the next Asset in the list to the current Asset, and returns it's filename.
72     * This can be used as a shortcut for SelectNextAsset(); GetFileName();
73     * @return A String which holds the name of the Asset set as the current Asset.
74     **/
75     public String GetNextFile()
76     {
77        //System.out.println("GetNextFile");
78        Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "GetNextFile");
79        SelectNextAsset();
80        return getValueAsString("location") + getValueAsString("filename");
81     }
82    
83    
84    /**
85    * SelectNextAsset
86    * Updates ImportData with the current Child Assets Data, 
87    * then advances to the next Asset in the ImportData Object
88    * @returns The index of the Asset set as current.
89     **/
90    public int SelectNextAsset()
91    {
92       int result = -1;
93       Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SelectNextAsset");
94       if(currentOutData != null && currentOut != null)
95       {
96         // First send the current set of data to the ScriptService to update ImportData
97         if(!service.processSection(currentOutData, currentOut)) //; // Add data from this section to ImportData
98         {
99             writeToConsole("An Asset has failed to be updated, it will now be deleted from the Import Data object");
100            deleteCurrentOutAsset();
101            //return -1;
102        }
103        System.out.println(currentOut.getFileName() + " Has been added/updated");
104        Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SelectNextAsset: " + currentOut.getFileName() + " Has been added/updated");
105        currentOut = null;
106        currentOutData = null;
107      }
108      //else
109        //Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SelectNextAsset: No Child Data available to update ImportData with.");
110      if(assetRecords != null)
111      {
112        if(index != assetRecords.size() - 1)
113        {
114             //if( SelectAssetByName(assetKeys[++index]) )
115             if( getAssetByPath((String)assetKeys.elementAt(++index)) )
116             {
117                result = index;
118             }
119         }
120         else
121         {
122             currentAsset = null;
123            currentData = null;
124         }
125         
126       }
127       Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SelectNextAsset:" + result);
128       return result;
129   }
130   
131   private boolean getAssetByPath(String sName)
132   {
133      if(sName != null)
134      {
135         // Get the next asset
136         ScriptServiceRecord record = (ScriptServiceRecord)assetRecords.get(sName);
137         if(record != null)
138         {
139               currentAsset = record.getAsset();
140               currentData = record.getData();
141               currentOut = currentAsset;
142               currentOutData = currentData;
143         }
144         if(currentAsset == null)
145         {
146            writeToConsole("Could not find an asset for " + sName);
147            index = -1;
148            return false;
149         }
150         writeToConsole("Current Asset: " + currentAsset.getFileName());
151         return true;
152      }
153      return false;
154      
155   }
156   /**
157   * SelectAssetByName
158    * Updates ImportData with the current Child Assets Data,
159    * then sets the currentAsset to the one specified by the value passed in as sName.
160   * @param sName The name of the file you wish to access the Asset for.
161   * @return boolean: true=success false=failure.
162   **/
163   public boolean SelectAssetByPath(String sName)
164   {
165        Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SelectAssetByName " + sName);
166        if(currentOutData != null && currentOut != null)
167        {
168            // First send the current set of data to the ScriptService to update ImportData
169            //service.processSection(currentOutData, currentOut);
170            if(!service.processSection(currentOutData, currentOut)) //; // Add data from this section to ImportData
171            {
172                  writeToConsole("An Asset has failed to be updated, it will now be deleted from the Import Data object");
173                  deleteCurrentOutAsset();
174                  //return -1;
175            }
176            Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SelectAssetByName: Updated ImportData with current Child Data.");
177            currentOut = null;
178            currentOutData = null;
179        }
180        else
181            Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SelectAssetByName: No Child Data available to update ImportData with.");
182        
183        if(getAssetByPath(sName))
184        {
185            index = assetKeys.indexOf(currentAsset);
186            Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SelectAssetByName:" + index);
187            return true;
188        }
189        index = -1;
190        return false;
191   }
192   
193   
194   /**
195   * GetValueAsString
196   * @param sKey The key for which the value is needed. IE: roleid, filename
197   * @return Returns the value stored in the currentData for the given key
198   * (currentData is a hashtable holding the key value pairs for the current asset
199   **/
200   public String getValueAsString(String sKey)
201   {
202        Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "GetValueAsString:" + sKey);
203        if(currentData != null)
204        {
205            Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "GetValueAsString:" + (String)currentData.get(sKey));   
206            return (String)currentData.get(sKey);
207        }
208        Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "GetValueAsString: null data");    
209        return null;
210   }
211   /**
212   * getValueAsInteger
213   * @param sKey The key for which the value is needed. IE: roleid, filename
214   * @return Returns the value stored in the currentData for the given key as an int value
215   * if the value cannot be converted to a String, -1 is returned.
216   **/
217   public int getValueAsInteger(String sKey)
218   {    
219        int nReturn = -1;
220        Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "getValueAsInteger:" + sKey);
221        if(currentData != null)
222        {
223            try{
224            Integer conv = new Integer((String)currentData.get(sKey));
225            nReturn = conv.intValue();
226            }catch(NumberFormatException e){}  
227        }
228        Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "getValueAsInteger:" + nReturn);
229        return nReturn;
230   }
231   
232   /**
233   * Completes the update process by sending the data for the current Child Asset,
234   * and setting things up to send it all back from whence it came.
235   * This should be used when your service is finished updating the ImportData Object.
236   **/
237   public boolean completeUpdate()
238   {
239        Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "updateComplete");
240        if(currentOutData != null && currentOut != null)
241        {
242            // First send the current set of data to the ScriptService to update ImportData
243            //service.processSection(currentOutData, currentOut);
244            if(!service.processSection(currentOutData, currentOut)) //; // Add data from this section to ImportData
245            {
246                  writeToConsole("An Asset has failed to be updated, it will now be deleted from the Import Data object");
247                  deleteCurrentOutAsset();
248                  return false;
249            }
250            else
251            {
252               Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "updateComplete: Updated ImportData with current Child Data.");
253               currentOutData = null;
254               currentOut = null;
255               return true;
256            }
257        }
258        else
259            Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "updateComplete: No data available for update.");
260        return false;
261   }
262   
263   /** 
264   * getCTLOptions
265   * Gets the [options] section of the .ctl file.
266   * @returns A string array with the contents of the [options] block from the .ctl file
267   **/
268   public String[] getCTLOptions()
269   {
270        Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "updateComplete");
271        return service.getCtlOptions();
272   }
273   
274   
275   /**
276   * GetFileName
277   * @return The Filename of the current Asset
278   **/
279   //public String GetFileName(String type)
280   public String GetFileName()
281   {
282      String sFilename = null;
283      Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "GetFileName");
284      if(currentData != null)
285      {
286        sFilename = (String)currentData.get("filename");
287        System.out.println("GetFileName " + sFilename); 
288      }
289      Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "GetFileName" + sFilename);
290      return sFilename;
291   }
292   
293   /**
294   * @param type This is now ignored 
295   * @return The name of the server for the current Asset.
296   * @deprecated Use getValueAsString.  
297   * DEPRECATED - Use getValueAsString.  IE: get Value As String of Frame theFrame using "server"
298   **/
299   public String GetServerName()
300   { 
301      String sName = null;
302      Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "GetServerName **DEPRECATED: Use getValueAsString.  IE: get Value As String of Frame theFrame using \"server\"");
303      if(currentData != null)
304      {
305        sName = (String)currentData.get("server");
306      }
307      System.out.println("GetServerName " + sName);
308      Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "GetServerName" + sName);
309      return sName; 
310   }
311   
312   
313   /**
314   * addChildAsset
315   * Creates a new DisguiseAssetRecordData object
316   * and adds it to the currentAsset as a child.
317   **/
318   public boolean addChildAsset()
319   {
320        boolean result = false;
321        System.out.println("addChildAsset...");
322        Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "addChildAsset");
323        currentOut = new DisguiseAssetRecordData(); // String
324        currentOutData = new Hashtable(); // reset the Hashtable for the new Asset
325        if(currentOut != null && currentAsset != null)
326        {
327            Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "New Asset Created ");
328            //Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "Adding to " + (String)currentData.get("filename"));
329            currentAsset.addChildAsset(currentOut);
330            currentOut.setBucketStructId(currentAsset.getBucketStructId());
331            currentOut.setServer(currentAsset.getServer());
332             currentOut.setAppleTalkVendor( currentAsset.getAppleTalkVendor() );
333            if(service.sFlagOut.equals("TEMP"))
334                currentOutData.put("isTemp", "true");
335            result = true;
336        }
337        if(result)// == 0)
338        {
339            Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "Added child asset to " + (String)currentData.get("filename"));
340        }
341        else
342            Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "Failed to add child asset to " + (String)currentData.get("filename"));
343        return result;
344   }
345   
346   public boolean deleteCurrentOutAsset()
347   {
348      if(currentOut != null )
349      {
350         
351         if(service.deleteAsset(currentOut))
352         {
353            currentOut = currentAsset;
354            currentData = currentOutData;     
355            return true;
356         }
357         /*if(currentAsset == currentOut )
358         {
359            Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, 9, "Deleting current asset " + currentAsset.getFileName());
360            service.deleteAsset(currentAsset);
361         }
362         else
363         {
364            Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, 9, "Deleting Child Asset " + currentAsset.getFileName());
365            currentAsset.deleteChildAsset(currentOut);
366         }*/
367         
368      }
369      writeToConsole("Could not delete asset");
370      return false;
371   }
372   
373   public boolean deleteCurrentAsset()
374   {
375      if(currentAsset != null )
376      {
377         String sFileName = currentAsset.getFileName();
378         if(service.deleteAsset(currentAsset))
379         {
380            currentAsset = null;
381            currentData = null;
382            currentOut = null;
383            currentOutData = null;
384            //assetKeys.removeElement(sFileName); 
385            assetRecords.put(sFileName, new ScriptServiceRecord(null,null));
386            //service.displayHash(assetRecords);
387            return true;
388         }
389      }
390      writeToConsole("Could not delete asset");
391      return false;
392   }
393   
394   
395   /**
396   * makeChildCurrent
397   * After updating ImportData with the currentData,
398   * The currentChild Asset (created by calling addChildAsset)
399   * is set to be the currentAsset.  This is done to create children 
400   * of children, so addChildAsset should be called after this.
401   **/
402   public boolean makeChildCurrent()
403   {
404        boolean result = false;
405        System.out.println("makeChildCurrent");
406        Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "makeChildCurrent:");
407        if(currentOut == null || currentAsset == null || currentOutData == null)
408        {
409            //Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "makeChildCurrent:" + result);
410            writeToConsole("Cannot make child current, either the current 'out' asset or the current 'in' asset is null");
411            return result;
412        }
413        // First send the current set of data to the ScriptService to update ImportData
414        //service.processSection(currentOutData, currentOut);
415        if(!service.processSection(currentOutData, currentOut)) //; // Add data from this section to ImportData
416        {
417               // Could not process data for this asset.
418               if(currentAsset != null )
419               {
420                  writeToConsole("An Asset has failed to be updated, it will now be deleted from the Import Data object");
421                  currentAsset.deleteChildAsset(currentOut);
422               }
423        }
424        else
425        {
426            currentAsset = currentOut;
427            currentData = currentOutData;
428            result = true;
429            Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "makeChildCurrent:" + result);
430        }
431        return result;
432   }
433 
434    /**
435    * Sets the value specified for the Asset field (key) specified,
436    * which will later be used to update the ImportData.
437    * @param String sData A key value pair in one string IE: filename=/somepath/to/a/File.TIF
438    **/
439    public boolean addData(String sData)
440    {
441         Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "addData:" + sData);
442         if(currentOutData != null)
443         {
444             currentOutData.put(service.getKey(sData).toLowerCase(), service.getValue(sData));
445             Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "addData: success");
446             return true;
447         }
448         Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "addData: failed");
449         return false;    
450    }
451   
452   /**
453   * Set the filename for the current Child Asset.
454   * Note: addChildAsset should be called prior to calling this method, 
455   * or there will be no Asset to set the filename for.  
456   * @deprecated use addData instead IE: add Data of Frame theFrame using \"filename=aFile.TIF\"");
457   **/
458   public boolean SetFileName(String fileName)
459   {
460        Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SetFileName " + fileName);
461        if(currentOutData != null)
462        {
463            currentOutData.put("filename", fileName);
464            Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SetFileName: success");   
465            return true;
466        }
467        Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SetFileName: failed");
468        return false;
469   }
470   
471   /**
472   * @deprecated Do not use; This is available only for backward compatibility.
473   *
474   public int SetImportType(String type)
475   {
476        Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SetImportType: DEPRECATED - No replacement");
477        return -1;
478   }*/
479   
480   /**
481   * SetImageType
482   * Will set the AssetType of the current Assets Role
483   * to the type specified
484   * @deprecated Use addData and pass in the role ID of the Role you want the 
485   * new Child asset to have. add Data of Frame theFrame using \"roleid=3\" (3 is for thumbnail)" );
486   **/
487   public int SetImageType(String type)
488   {
489      String sRoleId = "-1";
490      Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SetImageType: DEPRECATED - Use add Data of Frame theFrame using \"roleid=3\" (3 is for thumbnail)" );
491      if(type != null)
492         {
493             if(service.sRoleOut.toUpperCase().equals("HIGHRES"))
494                 sRoleId = "1";
495             else if(service.sRoleOut.toUpperCase().equals("LOWRES"))
496                 sRoleId = "2";
497             else if(service.sRoleOut.toUpperCase().equals("THUMBNAIL"))
498                 sRoleId = "3";
499             else if(service.sRoleOut.toUpperCase().equals("LAYOUT"))
500                 sRoleId = "4";
501             else if(service.sRoleOut.toUpperCase().equals("VIDEO"))
502                 sRoleId = "5";
503             else if(service.sRoleOut.toUpperCase().equals("AUDIO"))
504                 sRoleId = "6";
505                 
506             if(currentOutData != null)
507            {
508                currentOutData.put("roleid", sRoleId);
509                Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SetImageType:" + sRoleId);   
510                return 0;
511            }
512         }
513         Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SetImageType:" + sRoleId);
514         return -1;
515   }
516   
517   /**
518   * Sets the XRes value for the current Child Asset
519   * @deprecated Use addData instead. add Data of Frame theFrame using \"xres=100\"" );
520   **/
521   public boolean SetXRes(String xres)
522   {
523      Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SetXRes: DEPRECATED- Use add Data of Frame theFrame using \"xres=100\"" ); 
524      if(currentOutData != null)
525      {
526            currentOutData.put("xres", xres);
527            Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SetXRes:success");   
528            return true;
529      }
530      Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SetXRes:failed");
531      return false;
532   }
533   
534   /**
535    * Sets the YRes value for the current Child Asset.
536    * @deprecated Use addData instead. IE: addData("yres=100");
537    **/
538   public boolean SetYRes(String yres)
539   {
540        Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SetYRes:" + yres);  
541        if(currentOutData != null)
542        {
543            currentOutData.put("yres", yres);
544            Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SetYRes:success");   
545            return true;
546        }
547        Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SetYRes:failed");
548        return false;
549   }
550   
551   /**
552   * Sets the Width value for the current Child Asset.
553   * @deprecated Use addData instead. IE: addData("width=100");
554   **/
555   public boolean SetWidth(String width)
556   {
557        Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SetWidth:" + width);  
558        if(currentOutData != null)
559        {
560            currentOutData.put("width", width);
561            Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SetWidth:success");   
562            return true;
563        }
564        Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SetWidth:failed");
565        return false;
566   }
567   
568   /**
569   * Sets the Height value for the current Child Asset.
570   * @deprecated Use addData instead. IE: addData("height=100");
571   **/
572   public boolean SetHeight(String height)
573   {
574        Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SetHeight:" + height);  
575        if(currentOutData != null)
576        {
577            currentOutData.put("height", height);
578            Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SetHeight:0");   
579            return true;
580        }
581        Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SetHeight:-1");
582        return false;
583   }
584   
585   /**
586   * Sets the FileSize value for the current Child Asset.
587   * @deprecated Use addData instead. IE: addData("filesize=100"); 
588   **/
589   public boolean SetFileSize(String size)
590   {
591        Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SetFileSize:" + size);  
592        if(currentOutData != null)
593        {
594            currentOutData.put("size", size);
595            Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SetFileSize:0");   
596            return true;
597        }
598        Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SetFileSize:-1");
599        return false;
600   }
601   
602   /**
603   * @deprecated Do not use; This is available only for backward compatibility.
604   *
605   public void SetCopyToPath(String path)
606   {
607        Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "SetCopyToPath: DEPRECATED - unused" );  
608   }*/
609   
610   /**
611   * Allows applescripts to add text to the debug log
612   * Does not print to console window.
613   **/
614   public void logDebugMessage(String text)
615    {
616          Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, text);
617    }
618   
619   /**
620    * Provides FlexError support for Applescripts.
621    * writes text to Output console, debug log(indirectly), 
622    * and to the FlexDBServer error log.
623    **/
624   public void writeToErrorlog(String text)
625    {
626        service.logErrorMessage(text);
627    }
628   
629   /**
630    * Allows applescripts to write text to 
631    * the output console of the Remote Asset Server
632    * also write text to the debug log
633    **/
634   public void writeToConsole(String text)
635   {
636      service.writeToConsole(text);
637   }
638   
639   /**
640   * In previous versions a frame was used to display information about
641   * a script being run.  This was used to close the window.
642   *
643   public void closeWindow()
644   {
645      setVisible(false);
646      Diagnostic.trace(Diagnostic.CAT_SCRIPT_SVC, "Set visible false");
647   }*/
648   
649    public java.awt.Dimension getPreferredSize()
650   {
651     return new java.awt.Dimension(0,0);
652   }
653   
654 }