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

Quick Search    Search Deep

Source code: org/acs/damsel/client/add/AddAssetAction.java


1   package org.acs.damsel.client.add;
2   
3   import java.io.*;
4   import java.util.Vector;
5   import java.util.Enumeration;
6   import javax.servlet.http.*;
7   
8   import org.acs.damsel.client.*;
9   import org.acs.damsel.srvr.*;
10  import org.acs.damsel.srvr.asset.*;
11  import org.acs.damsel.srvr.collection.*;
12  import org.acs.damsel.srvr.repository.*;
13  import org.apache.log4j.*;
14  import org.apache.struts.action.*;
15  import org.apache.struts.upload.*;
16  import org.acs.damsel.srvr.user.*;
17  import org.acs.damsel.srvr.collection.*;
18  
19  public class AddAssetAction
20      extends Action {
21  
22    private Logger log = Logger.getLogger(AddAssetAction.class);
23  
24    public AddAssetAction() {
25      BasicConfigurator.resetConfiguration();
26      PropertyConfigurator.configure(Config.instance().getLogPropertiesFileName());
27    }
28  
29    public ActionForward execute(ActionMapping actionMapping,
30                                 ActionForm actionForm,
31                                 HttpServletRequest httpServletRequest,
32                                 HttpServletResponse httpServletResponse) {
33  
34      int numAdded = 0;
35      AddAssetForm addAssetForm = (AddAssetForm) actionForm;
36      String addAssetFileName = addAssetForm.getAssetFile().getFileName();
37      String addWebFileName = addAssetForm.getWebViewFile().getFileName();
38      AddSingleAssetForm addSingleAssetForm = (AddSingleAssetForm)
39          httpServletRequest.getSession().getAttribute("addSingleAssetForm");
40  
41      String cName = addSingleAssetForm.getCollectionSelect();
42  
43      String descriptorValue = null;
44      String descriptorName = null;
45  
46      Asset a = new Asset();
47      AssetDescriptor ad;
48      AssetDescriptorCollection adc = new AssetDescriptorCollection();
49      ActionErrors errors = new ActionErrors();
50      Vector nameVec = new Vector();
51      Vector valueVec = new Vector();
52  
53      // The input forms on the add asset page are dynamically generated according
54      // to the Schema associated with the Collection that the User selects from
55      // a drop-down box.  The form names are prefixed with "form_" to distinguish
56      // them from other parameters, like the submit button.
57      // This loops through the list of parameters and picks off the form entries.
58      // It then creates an AssetDescriptor that contains the name of the form
59      // minus the "form_" part (which corresponds to the name as given in the
60      // Schema) and a value equal to whatever the user typed into the form.
61      // Note that this does not currently do any validation.
62      // Each of these AssetDescriptors are in turn added to the
63      // AssetDescriptorCollection, which is itself added to the Asset after all
64      // of the parameters have been exhausted.
65      for (Enumeration e = httpServletRequest.getParameterNames(); e.hasMoreElements(); ) {
66        String potentialName = (String) e.nextElement();
67        if (potentialName.substring(0, 5).equals("form_")) {
68          ad = new AssetDescriptor();
69          descriptorName = potentialName.substring(5);
70          descriptorValue = (String) httpServletRequest.getParameter(
71              potentialName);
72          if (descriptorName.indexOf("FileName") != -1)
73            descriptorValue = descriptorValue.replaceAll("'", "");
74          nameVec.addElement(descriptorName);
75          ad.setTag(descriptorName);
76          valueVec.addElement(descriptorValue);
77          ad.setValue(descriptorValue);
78          adc.addAssetDescriptor(ad);
79        }
80      }
81  
82      nameVec.addElement("FilePath");
83      valueVec.addElement(Config.instance().getAssetPrefix() +
84                          addAssetForm.getAssetFile().getFileName());
85  
86      // Inserting the asset's owner
87      User user = (User) httpServletRequest.getSession().getAttribute("User");
88      ad = new AssetDescriptor();
89      ad.setTag("OwnerName");
90      ad.setValue(user.getUserName());
91      adc.addAssetDescriptor(ad);
92  
93      // Inserting the asset's permissionID (obtained from its collection's permissionID)
94      ad = new AssetDescriptor();
95      ad.setTag("PermissionID");
96  
97      String id = null;
98      id = ClientApp.instance().getCollectionMgr().getCollection(cName).
99          getPermissionID();
100     ad.setValue(id);
101     adc.addAssetDescriptor(ad);
102 
103     // Inserting the asset's group name (obtained from the collection's group name)
104 
105     ad = new AssetDescriptor();
106     ad.setTag("GroupName");
107     ad.setValue(ClientApp.instance().getCollectionMgr().getCollection(cName).
108                 getGroupName());
109     adc.addAssetDescriptor(ad);
110 
111     /*The only fields in addSingleAsset that are mandatory is the fileName we
112      can't add an asset without an asset filename! So if the user
113          didn't supply the path of the asset to be uploaded, we can't continue and must
114      display an error message. Note that the webFileName is optional.*/
115     String fileName = adc.getValue("FileName");
116     if (fileName == null || fileName.trim().equals("")) {
117       errors = new ActionErrors();
118       errors.add("asset", new ActionError("asset.fileName.not.given"));
119       this.saveErrors(httpServletRequest, errors);
120       return actionMapping.findForward("errorPage");
121     }
122 
123     /*If the WebFileName field is filled in, the user MUST select an asset's web view,
124      and vice-versa. so we need to check for that and display an appropriate error message*/
125     String webFileName = adc.getValue("WebFileName");
126 
127     if ( ( (webFileName != null) && (!webFileName.trim().equals("")) &&
128           (addWebFileName.trim().equals("")))
129         ||
130         ( (webFileName != null) && (webFileName.trim().equals("")) &&
131          (!addWebFileName.trim().equals("")))) {
132       errors = new ActionErrors();
133       errors.add("asset", new ActionError("webFileName.not.given"));
134       this.saveErrors(httpServletRequest, errors);
135       return actionMapping.findForward("errorPage");
136     }
137 
138     /*If the user gave the same file name for webFileName and FileName we need to shout
139          at them! */
140     if (fileName.equals(webFileName)) {
141       errors.add("asset", new ActionError("addasset.duplicateFileName"));
142       this.saveErrors(httpServletRequest, errors);
143       return actionMapping.findForward("errorPage");
144     }
145 
146     /* If the user did not enter a webFileName or select a webFile, we need to copy
147          the fileName into the WebFileName AD. However, before we add the WebFileName,
148      we need to remove any webFileName AD's that may already be in the file. */
149 
150     if ( (webFileName == null || webFileName.trim().equals("")) &&
151         (addWebFileName == null || addWebFileName.trim().equals(""))) {
152       // Copy the value of filename into webfilename
153       adc.getAssetDescriptor("WebFileName").setValue(fileName);
154     }
155 
156     valueVec.set(nameVec.indexOf("WebFileName"), adc.getAssetDescriptor("WebFileName").getValue());
157     httpServletRequest.getSession().setAttribute("nameVec", nameVec);
158     httpServletRequest.getSession().setAttribute("valueVec", valueVec);
159 
160 
161     // Once the Asset has been filled out, it is added to the Collection
162     // specified by the User.
163     // Note that this currently uses a default owner name rather than extracting
164     // one from the page or the database.  It also performs no User
165     // authorization.
166     try {
167       a.setAssetDescriptors(adc);
168       RepositoryMgr rmgr = ClientApp.instance().getRepositoryMgr();
169       numAdded = rmgr.addAsset(a, cName);
170 
171       //if asset is already in collection, show error message
172       if (numAdded == 0) {
173         errors.add("asset", new ActionError("asset.already.exists"));
174         this.saveErrors(httpServletRequest, errors);
175         return actionMapping.findForward("errorPage");
176       }
177       doUpload(addAssetForm.getAssetFile(), a.getFileName());
178       if (!(addWebFileName == null || addWebFileName.trim().equals("")))
179         doUpload(addAssetForm.getWebViewFile(), a.getWebFileName());
180     }
181     catch (Exception ex) {
182       log.error("Exception " + ex.getMessage());
183       ex.printStackTrace();
184     }
185     // afterward, the User is forwarded to a confirmation page that allows
186     // them to return to the add asset page to add more assets.
187     return actionMapping.findForward("confirmation");
188   }
189 
190   // this method writes the uploaded file to a new file with the given fileName
191   public void doUpload(FormFile theFile, String fileName) throws IOException {
192     log.info("About to write: " + theFile.getFileSize() + " bytes to " +
193              Config.instance().getUploadPrefix() + fileName);
194     FileOutputStream out = new FileOutputStream(new File(Config.instance().
195         getUploadPrefix() +
196         fileName));
197     out.write(theFile.getFileData(), 0, theFile.getFileSize());
198     out.close();
199   }
200 }