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
29
30 import org.apache.cocoon.servlet.multipart.*;
31 import java.io.File;
32
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
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
90
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
98
99 DocumentBuilder registryBuilder = null;
100 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
101 registryBuilder = dbf.newDocumentBuilder();
102
103
104
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
112 ras = RegistryDelegateFactory.createAdmin();
113
114 harvestDoc = registryBuilder.parse(accessURL);
115
116
117 resultDoc = ras.update(harvestDoc);
118
119
120
121
122
123 }
124 } else if(request.getParameter("addmetadatafromfile") != null) {
125
126
127 Part part = (Part) request.get("metadata_file");
128 if (part != null) {
129 ras = RegistryDelegateFactory.createAdmin();
130 harvestDoc = registryBuilder.parse(part.getInputStream());
131
132
133 resultDoc = ras.update(harvestDoc);
134
135
136 } else {
137
138 }
139
140
141
142
143
144 }
145 }catch(Exception e) {
146 e.printStackTrace();
147 errorMessage = e.toString();
148 }
149
150 System.out.println("leaving admin action");
151
152
153
154 results.put("message",message);
155 results.put("errorMessage",errorMessage);
156
157 return results;
158 }
159
160 }