View Javadoc

1   /***
2    * Created on Aug 5, 2005, Copyright UC Regents
3    */
4   package wise2.converter.converters;
5   
6   import java.beans.BeanInfo;
7   import java.io.File;
8   import java.io.FileInputStream;
9   import java.io.InputStream;
10  import java.net.URI;
11  import java.net.URISyntaxException;
12  import java.net.URL;
13  import java.util.HashMap;
14  import java.util.List;
15  import java.util.Map;
16  import java.util.Set;
17  import java.util.Map.Entry;
18  
19  import net.sf.sail.core.entity.Rim;
20  
21  import org.concord.datagraph.state.OTDataDrawingToolView;
22  import org.concord.framework.otrunk.OTObjectList;
23  import org.concord.framework.otrunk.OTrunk;
24  import org.concord.graph.util.state.OTDrawingTool;
25  import org.concord.graph.util.state.OTDrawingTool2;
26  import org.concord.otrunk.OTSystem;
27  import org.concord.otrunk.OTrunkImpl;
28  import org.concord.otrunk.datamodel.OTDataObject;
29  import org.concord.otrunk.datamodel.OTDatabase;
30  import org.concord.otrunk.view.OTViewEntry;
31  import org.concord.otrunk.view.OTViewService;
32  import org.concord.otrunk.view.document.OTCompoundDoc;
33  import org.concord.otrunk.view.document.OTDocumentView;
34  import org.concord.otrunk.xml.Exporter;
35  import org.concord.otrunk.xml.XMLBlobResource;
36  import org.concord.otrunk.xml.XMLDatabase;
37  import org.dom4j.Node;
38  import org.telscenter.pas.beans.PasStep;
39  import org.telscenter.pas.otrunk.BeanIcons;
40  import org.telscenter.pas.otrunk.OTrunkStep;
41  import org.telscenter.pas.steps.icons.PasStepIconProvider;
42  
43  import wise2.converter.AbstractStepConverter;
44  import wise2.converter.util.HTMLUtils;
45  
46  /***
47   * @author turadg
48   */
49  public class Wisedraw2Converter extends AbstractStepConverter {
50  
51  	private static final String CONVERSION_UNSPECIFIED = "CONVERSION_UNSPECIFIED";
52  
53  	@Override
54  	public PasStep getTypedPasStep() {
55  		OTrunkStep step = new OTrunkStep();
56  		
57  		/*
58  		 * The java code below creates this xml file
59  		 * 
60  <otrunk>
61    <imports>
62      <import class="org.concord.otrunk.OTSystem"/>
63      <import class="org.concord.otrunk.view.OTViewEntry"/>
64      <import class="org.concord.otrunk.view.OTViewService"/>
65      <import class="org.concord.graph.util.state.OTDrawingTool"/>
66      <import class="org.concord.otrunk.view.document.OTCompoundDoc"/>
67    </imports>
68    <objects>
69      <OTSystem>
70        <services>
71          <OTViewService>
72            <viewEntries>
73              <OTViewEntry
74                objectClass="org.concord.graph.util.state.OTDrawingTool" 
75                viewClass="org.concord.datagraph.state.OTDataDrawingToolView"/>
76              <OTViewEntry
77                objectClass="org.concord.otrunk.view.document.OTDocument" 
78                viewClass="org.concord.otrunk.view.document.OTDocumentView"/>
79            </viewEntries>
80          </OTViewService>
81        </services>
82        <root>
83          <OTCompoundDoc>
84            <bodyText>
85              Hello World<br/>
86              <object refid="${drawing1}"/>
87            </bodyText>
88            <documentRefs>
89              <OTDrawingTool local_id="drawing1"/> 
90            </documentRefs>
91          </OTCompoundDoc>      
92        </root>
93      </OTSystem>
94    </objects>
95  </otrunk>
96  		 */
97  		
98  		// Need to create the OTrunk state here
99  		OTDatabase otDb = new XMLDatabase();
100 		
101 		try {
102 			OTrunk otrunk = new OTrunkImpl(otDb);
103 			OTSystem system = (OTSystem)otrunk.createObject(OTSystem.class);
104 			otrunk.setRoot(system);
105 
106 			OTObjectList services = system.getServices();
107 			OTViewService viewService = 
108 				(OTViewService)otrunk.createObject(OTViewService.class);
109 			services.add(viewService);
110 
111 			OTViewEntry viewEntry;
112 			// Create drawing tool view mapping
113 			viewEntry = (OTViewEntry)otrunk.createObject(OTViewEntry.class);
114 			viewEntry.setObjectClass(OTDrawingTool.class.getName());
115 			viewEntry.setViewClass(OTDataDrawingToolView.class.getName());
116 			viewService.getViewEntries().add(viewEntry);
117 
118 			// Create document view mapping
119 			viewEntry = (OTViewEntry)otrunk.createObject(OTViewEntry.class);
120 			viewEntry.setObjectClass(OTCompoundDoc.class.getName());
121 			viewEntry.setViewClass(OTDocumentView.class.getName());
122 			viewService.getViewEntries().add(viewEntry);
123 
124 			OTCompoundDoc compoundDoc =
125 				(OTCompoundDoc)otrunk.createObject(OTCompoundDoc.class);
126 			system.setRoot(compoundDoc);
127 						
128 			OTDrawingTool drawingTool = 
129 				(OTDrawingTool)otrunk.createObject(OTDrawingTool2.class);
130 			
131 			Map<String, URI> backgrounds = getNamedUris("backgrounds");
132 			//Map<String, URI> stamps = getNamedUris("stamps");
133 
134 			Set<Entry<String, URI>> entries = backgrounds.entrySet();
135 
136 			if(entries.size() > 0) {
137 				Entry<String, URI> entry = entries.iterator().next();
138 
139 				URL backgroundURL = entry.getValue().toURL();
140 
141 				// This is a hack because there isn't a better way yet
142 				XMLBlobResource blobResource = new XMLBlobResource(backgroundURL);
143 				OTDataObject drawingToolDO = 
144 					otDb.getOTDataObject(null, drawingTool.getGlobalId());
145 				drawingToolDO.setResource("backgroundImage", blobResource);
146 			}
147 
148 			String instructionText = getInstructionText();
149 						
150 			// Strip off the header
151 			instructionText = 
152 				instructionText.replaceFirst("(?s).*<body>(.*)</body>.*", "$1");
153 
154 			instructionText += "<object refid=\"" +
155 				drawingTool.getGlobalId() + "\"/>";
156 			compoundDoc.setBodyText(instructionText);
157 						
158 			// now we need to write out this database to a file and then 
159 			// refer to that file url
160 			File outputFile = File.createTempFile("drawing", "otml");
161 			Exporter.export(outputFile, otDb.getRoot(), otDb);
162 
163 			InputStream otmlInput = new FileInputStream(outputFile);
164 			URL podURL = addToArchive("drawing.otml", otmlInput);
165 			
166 			step.setAuthoredDataURL(podURL);
167 			
168 			// Setup the icons for this step			
169 			URL iconUrl;
170 			BeanIcons icons = new BeanIcons();
171 			
172 			iconUrl = PasStepIconProvider.getIconUrl("draw", BeanInfo.ICON_COLOR_16x16);
173 			if(iconUrl != null){			
174 				InputStream iconInput = iconUrl.openStream(); 
175 				URL podUrl = addToArchive(iconUrl.getFile(), iconInput);
176 				icons.setColor16x16(podUrl);
177 			}		
178 			iconUrl = PasStepIconProvider.getIconUrl("draw", BeanInfo.ICON_COLOR_32x32);
179 			if(iconUrl != null){			
180 				InputStream iconInput = iconUrl.openStream(); 
181 				URL podUrl = addToArchive(iconUrl.getFile(), iconInput);
182 				icons.setColor32x32(podUrl);
183 			}
184 			iconUrl = PasStepIconProvider.getIconUrl("draw", BeanInfo.ICON_MONO_16x16);
185 			if(iconUrl != null){			
186 				InputStream iconInput = iconUrl.openStream(); 
187 				URL podUrl = addToArchive(iconUrl.getFile(), iconInput);
188 				icons.setMono16x16(podUrl);
189 			}
190 			iconUrl = PasStepIconProvider.getIconUrl("draw", BeanInfo.ICON_MONO_32x32);
191 			if(iconUrl != null){			
192 				InputStream iconInput = iconUrl.openStream(); 
193 				URL podUrl = addToArchive(iconUrl.getFile(), iconInput);
194 				icons.setMono32x32(podUrl);
195 			}
196 		} catch (Exception e){
197 			e.printStackTrace();
198 		}
199 		
200 		// FIXME need to take care of the introduction html
201 		// can try putting it in OTDocument later
202 		// step.setIntroductionHtml(getInstructionText());
203 
204 
205 		
206 		Rim rim = new Rim();
207 
208 		//rim.setShape(String.class);
209 		rim.setShape(byte[].class);
210 		rim.setName("otrunk_drawing");
211 		step.setWorkRim(rim); 
212 		getPod().add(rim);
213 
214 		return step;
215 	}
216 
217 	/***
218 	 * @return instruction text to use in this step
219 	 */
220 	private String getInstructionText() {
221 		Node htmlNode = stepNode.selectSingleNode("parameters/html");
222 		String instructionText = (htmlNode == null) ? CONVERSION_UNSPECIFIED
223 				: htmlNode.getText();
224 		
225 		instructionText = HTMLUtils.tidyHtml(instructionText, "Wisedraw2" );
226 		
227 		return instructionText;
228 	}
229 
230 	// FIXME embed these external URIs into the pod and use those new URIs
231 	private Map<String, URI> getNamedUris(String type) {
232 		Map<String, URI> map = new HashMap<String, URI>();
233 		String xpath = "parameters/" + type + "/*";
234 
235 		// This xml implementation doesn't appear to support generics
236 		@SuppressWarnings("unchecked")
237 		List<Node> backgroundNodes = stepNode.selectNodes(xpath);
238 		
239 		for (Node node : backgroundNodes) {
240 			String name = node.getName();
241 			String uriStr = node.getText();
242 			try {
243 				URI uri = new URI(uriStr);
244 				map.put(name, uri);
245 			} catch (URISyntaxException e) {
246 				e.printStackTrace();
247 			}
248 		}
249 		return map;
250 	}
251 
252 }