View Javadoc

1   package org.astrogrid.portal.registry;
2   
3   import org.apache.avalon.framework.parameters.Parameters;
4   import org.apache.cocoon.acting.AbstractAction;
5   import org.apache.cocoon.environment.Request;
6   import org.apache.cocoon.environment.Session;
7   import org.apache.cocoon.environment.Redirector;
8   import org.apache.cocoon.environment.SourceResolver;
9   import org.apache.cocoon.environment.ObjectModelHelper;
10  import org.w3c.dom.Document;
11  import org.w3c.dom.NodeList;
12  import org.w3c.dom.Node;
13  import org.w3c.dom.NamedNodeMap;
14  import java.util.Map;
15  import java.util.HashMap;
16  import java.io.Reader;
17  import java.io.StringReader;
18  import org.xml.sax.InputSource;
19  import java.text.SimpleDateFormat;
20  import java.util.Date;
21  import java.text.ParseException;
22  import org.astrogrid.registry.client.RegistryDelegateFactory;
23  import org.astrogrid.registry.client.admin.RegistryAdminService;
24  import org.astrogrid.registry.client.harvest.RegistryHarvestService;
25  import javax.xml.parsers.DocumentBuilderFactory;
26  import javax.xml.parsers.DocumentBuilder;
27  import org.astrogrid.util.DomHelper;
28  //import org.apache.cocoon.components.request.multipart.FilePart;
29  //import org.apache.cocoon.components.request.multipart.FilePartFile;
30  import org.apache.cocoon.servlet.multipart.*;
31  import java.io.File;
32  //import org.apache.cocoon.servlet.multipart.Part;
33  
34  
35  
36  
37  
38  /***
39   * Used for harvesting Metadata from another registry.  Depending on the request may harvest all metadata or
40   * harvest a paricular registry data.
41   *
42   */
43  public class RegistryAdminAction extends AbstractAction
44  {
45     /***
46      * Switch for our debug statements.
47      *
48      */
49     public static boolean DEBUG_FLAG = true;
50     
51     /***
52      * Cocoon param for the user param in the session.
53      *
54      */
55     private static final String PARAM_ACTION = "action";
56     
57     private static final String PARAM_CRITERIA_NUMBER = "criteria_number";   
58  
59     private static final String PARAM_MAIN_SEARCH_ELEMENT = "searchelement";   
60     
61     private static final Integer DEFAULT_CRITERIA_NUMBER = new Integer(1);
62     
63     private static final String HARVEST_THIS_REGISTRY_ACTION = "harvestthis";
64     
65     private static final String HARVEST_OTHER_REGISTRY_ACTION = "harvestother";   
66     
67     private static final String ERROR_MESSAGE = "errormessage";   
68        
69     /***
70      * Our action method.
71      *
72      */
73     public Map act(
74        Redirector redirector, 
75        SourceResolver resolver, 
76        Map objectModel, 
77        String source, 
78        Parameters params)
79        {
80        
81        //
82        // Get our current request and session.
83        Request request = ObjectModelHelper.getRequest(objectModel);
84        Session session = request.getSession();
85        Map results = new HashMap() ;
86  
87        String errorMessage = null;
88        String message = null;
89        //Was this coming from the query page with a huge xml string
90        //containing how to call the registry.
91        Document harvestDoc = null;
92        String harvestResult = "";
93        Document resultDoc = null;
94        RegistryAdminService ras = null;
95        System.out.println("inside action of admin");
96        try {
97           //get where you putting the harvest results to.
98              
99                 DocumentBuilder registryBuilder = null;
100                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
101                registryBuilder = dbf.newDocumentBuilder();
102                
103                //Now check are calling updateDocument or addRegistryEntries.
104                //TODO do this portal thing.
105                if(request.getParameter("addmetadatafromurl") != null) {
106                   String accessURL = request.getParameter("metadata_url");
107                   if(accessURL == null || accessURL.trim().length() <= 0) {
108                      errorMessage = "A URL was not given for harvesting.";
109                   }
110                   if(errorMessage == null) {
111                      //instantiate the delegate.
112                      ras = RegistryDelegateFactory.createAdmin();
113                      
114                      harvestDoc = registryBuilder.parse(accessURL);
115                      
116                      //ras.harvestFromUrl(accessURL);
117                      resultDoc = ras.update(harvestDoc);
118                      //ras.validateDocument(harvestDoc);
119                      //Now see if their is a error element in the resultDoc
120                      //ras.addRegistryEntries(harvestDoc);
121                      //message = "A harvest has begun for url = " + accessURL;
122                      //results.put("addregistry","true");
123                   }//if                  
124                } else if(request.getParameter("addmetadatafromfile") != null) {
125                   //request.ge
126   //                FilePart filePart = (FilePart) request.get("metadata_file");
127                      Part part = (Part) request.get("metadata_file");
128                      if (part != null) {
129                         ras = RegistryDelegateFactory.createAdmin();
130                         harvestDoc = registryBuilder.parse(part.getInputStream());
131                         //System.out.println("the harvestdoc TEST = " + DomHelper.DocumentToString(harvestDoc));
132                         //System.out.println("the filename = " + part.getFileName() + " the size = " + part.getSize() + " and harvestDoc = " + DomHelper.DocumentToString(harvestDoc));
133                         resultDoc = ras.update(harvestDoc);
134                         //ras.validateDocument(harvestDoc);
135                         // do something with it
136                      } else {
137                         // parameter not found
138                      }
139   
140   //                File file = ((FilePartFile)filePart).getFile();
141   //                ras = RegistryDelegateFactory.createAdmin();
142   //                harvestDoc = registryBuilder.parse(file);
143   //                resultDoc = ras.update(harvestDoc);
144                }
145       }catch(Exception e) {
146          e.printStackTrace();
147          errorMessage = e.toString();         
148       }
149       
150       System.out.println("leaving admin action");
151       //
152       //Create a new HashMap for our results.  Will be used to
153       //pass to the transformer (xsl page)
154       results.put("message",message);
155       results.put("errorMessage",errorMessage);
156 
157       return results;
158    }
159         
160 }