source: MDService2/trunk/MDService2/src/eu/clarin/cmdi/mdservice/action/SaveWorkspaceAction.java

Last change on this file was 684, checked in by gaba, 14 years ago
File size: 6.1 KB
Line 
1package eu.clarin.cmdi.mdservice.action;
2
3
4import org.apache.commons.lang.*;
5import com.opensymphony.xwork2.ActionSupport;
6import net.sf.json.JSON;
7import net.sf.json.JSONArray;
8import net.sf.json.JSONObject;
9import net.sf.json.JSONSerializer;
10import net.sf.json.JSONException;
11import net.sf.json.xml.XMLSerializer;
12
13import java.io.BufferedInputStream;
14import java.io.File;
15import java.io.FileInputStream;
16import java.io.FileWriter;
17import java.io.IOException;
18import java.io.InputStream;
19import java.io.StringReader;
20import java.io.StringWriter;
21import java.net.MalformedURLException;
22import java.net.URL;
23import java.util.HashMap;
24import java.io.ByteArrayInputStream;
25
26import javax.xml.parsers.DocumentBuilder;
27import javax.xml.parsers.DocumentBuilderFactory;
28import javax.xml.parsers.ParserConfigurationException;
29import javax.xml.transform.OutputKeys;
30import javax.xml.transform.Transformer;
31import javax.xml.transform.TransformerConfigurationException;
32import javax.xml.transform.TransformerException;
33import javax.xml.transform.TransformerFactory;
34import javax.xml.transform.TransformerFactoryConfigurationError;
35import javax.xml.transform.dom.DOMSource;
36import javax.xml.transform.stream.StreamResult;
37
38import org.apache.commons.io.IOUtils;
39import org.w3c.dom.Document;
40import org.w3c.dom.Element;
41import org.w3c.dom.Node;
42import org.w3c.dom.NodeList;
43import org.xml.sax.InputSource;
44import org.xml.sax.SAXException;
45
46import eu.clarin.cmdi.mdservice.model.Query;
47
48/**
49 * Query load , save from PROFILE
50 * 
51 * @author
52 *
53 */
54public class SaveWorkspaceAction extends GenericProxyAction {
55
56        /**
57         *
58         */
59        private static final long serialVersionUID = 1L;
60        private String q;
61        private String id;
62        private String base_path;
63       
64         
65        /*
66        public XMLSerializer getXMLSerializer(){
67                if (serializer == null){
68                        serializer = new XMLSerializer();
69                }
70                return serializer;
71        }
72        public JSONSerializer getJSONSerializer(){
73                if (json_serializer == null){
74                        json_serializer = new JSONSerializer();
75                }
76                return json_serializer;
77        }
78        */
79        public String getQ() {
80                return q;
81        }
82
83        public void setQ(String q) {
84                this.q = q;
85        }
86
87        public String getID() {
88                return id;
89        }
90
91        public void setID(String _id) {
92                this.id = _id;
93        }
94        public String getWorkspaceProfile() {
95                return "WorkspaceProfile.xml";
96        }
97       
98        public String getBasePath() {           
99                if (base_path == null) {
100                        base_path = Admin.getConfig().getProperty("query.path");
101                }
102                return base_path;
103        }
104        public String getPath() {
105                String targetPath = getBasePath() + getWorkspaceProfile();         
106                     
107        return targetPath;
108        }
109       
110       
111        public String getProxyKey() {
112                return "";
113        }
114       
115        public String getFullFormat() {
116                return "";
117        }
118       
119        public void changeNodeName(Document doc, Node node, String new_name){
120                // Create an element with the new name
121                Node node2 = (Node ) doc.createElement(new_name); 
122                // Copy the attributes to the new element NamedNodeMap attrs = element.getAttributes(); for (int i=0; i<attrs.getLength(); i++) { Attr attr2 = (Attr)doc.importNode(attrs.item(i), true); element2.getAttributes().setNamedItem(attr2); }
123                // Move all the children
124                while (node.hasChildNodes()) { node2.appendChild(node.getFirstChild()); } 
125                // Replace the old node with the new node
126                Admin.notifyUser("replace:" + node2.getNodeName() + "," + node.getNodeName());
127                node.getParentNode().replaceChild(node2, node); 
128        }
129        public void prepare(){
130               
131        String jsonData = getQ();
132        Admin.notifyUser("JSON data:"+jsonData);
133               
134        XMLSerializer serializer = new XMLSerializer();
135                JSON json = JSONSerializer.toJSON( jsonData );
136                //Admin.notifyUser("test5");
137                serializer.setRootName("WorkspaceProfile");
138        serializer.setTypeHintsEnabled(false);
139        String xml = serializer.write( json );
140        Admin.notifyUser("XML:" + xml);
141       
142       
143        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
144        DocumentBuilder builder;
145                try {
146                        builder = factory.newDocumentBuilder();
147                        try {
148                                Document  document = builder.parse(new InputSource(new StringReader(xml)));
149                                NodeList list = document.getElementsByTagName("e");
150                                Admin.notifyUser("list:" + list.getLength());
151                                while (list.getLength() > 0){
152                                        Admin.notifyUser(0 + "::" + list.item(0).getNodeName() + "::" + list.item(0).getParentNode().getNodeName());
153                                        if (list.item(0).getParentNode().getNodeName().equals("Queries")){
154                                                changeNodeName(document, list.item(0),"Query"); 
155                                        }else{
156                                                if (list.item(0).getParentNode().getNodeName().equals("Querysets")){
157                                                        changeNodeName(document, list.item(0),"Queryset");
158                                                } else{
159                                                        if (list.item(0).getParentNode().getNodeName().equals("collections")){
160                                                                changeNodeName(document, list.item(0),"item");
161                                                        }
162                                                }
163                                        }
164                                        list = document.getElementsByTagName("e");
165                                }
166                               
167                                Transformer transformer;
168                                try {
169                                        transformer = TransformerFactory.newInstance().newTransformer();
170                                        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
171                                        StreamResult result = new StreamResult(new StringWriter());
172                                        DOMSource source = new DOMSource(document);
173                                        try {
174                                                transformer.transform(source, result);
175                                                String xmlString = result.getWriter().toString();
176                                               
177                                                Admin.notifyUser("XML:" + xmlString);
178                                       
179                                        InputStream is_xml = new ByteArrayInputStream( xmlString.getBytes( ) );
180                                        Admin.writeToFile(getPath(), is_xml);
181                                       
182                                        } catch (TransformerException e1) {
183                                                // TODO Auto-generated catch block
184                                                e1.printStackTrace();
185                                        }
186
187                                       
188                                       
189                                } catch (TransformerConfigurationException e) {
190                                        // TODO Auto-generated catch block
191                                        e.printStackTrace();
192                                } catch (TransformerFactoryConfigurationError e) {
193                                        // TODO Auto-generated catch block
194                                        e.printStackTrace();
195                                }
196                               
197                        } catch (SAXException e) {
198                                // TODO Auto-generated catch block
199                                e.printStackTrace();
200                        } catch (IOException e) {
201                                // TODO Auto-generated catch block
202                                e.printStackTrace();
203                        }
204                } catch (ParserConfigurationException e) {
205                        // TODO Auto-generated catch block
206                        e.printStackTrace();
207                }
208       
209       
210        }
211       
212        public String execute() throws Exception {
213               
214                prepare();
215                return SUCCESS;
216               
217               
218        }
219}
Note: See TracBrowser for help on using the repository browser.