source: MDService2/branches/MDService_simple/src/eu/clarin/cmdi/mdservice/action/SaveWorkspaceAction.java @ 1512

Last change on this file since 1512 was 1512, checked in by vronk, 13 years ago

troubles commiting - trying once again:
dynamic handling of request-params.
basic static and repo request should work

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