View Javadoc

1   /*
2    WSMO Studio - a Semantic Web Service Editor
3    Copyright (c) 2004-2007, Ontotext Lab. / SIRMA Group
4    
5    This library is free software; you can redistribute it and/or modify it under
6    the terms of the GNU Lesser General Public License as published by the Free
7    Software Foundation; either version 2.1 of the License, or (at your option)
8    any later version.
9    This library is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   details.
13   You should have received a copy of the GNU Lesser General Public License along
14   with this library; if not, write to the Free Software Foundation, Inc.,
15   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16   */
17  
18  /***
19   * <p>Title: WSMO Studio</p>
20   * <p>Description: Semantic Web Service Editor</p>
21   * <p>Copyright:  Copyright (c) 2004-2007</p>
22   * <p>Company: Ontotext Lab. / SIRMA </p>
23   */
24  
25  package org.semanticgov.ui.wizards;
26  
27  import java.io.*;
28  
29  import org.eclipse.core.resources.*;
30  import org.eclipse.core.runtime.CoreException;
31  import org.eclipse.core.runtime.IPath;
32  import org.eclipse.jface.dialogs.MessageDialog;
33  import org.eclipse.jface.viewers.IStructuredSelection;
34  import org.eclipse.jface.wizard.Wizard;
35  import org.eclipse.ui.*;
36  import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
37  import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;
38  import org.omwg.ontology.Ontology;
39  import org.semanticgov.ui.Activator;
40  import org.semanticgov.ui.Constants;
41  import org.wsmo.common.IRI;
42  import org.wsmo.common.TopEntity;
43  import org.wsmo.common.exception.InvalidModelException;
44  import org.wsmo.factory.WsmoFactory;
45  import org.wsmo.service.*;
46  import org.wsmo.wsml.Serializer;
47  import org.wsmostudio.runtime.LogManager;
48  import org.wsmostudio.runtime.WSMORuntime;
49  import org.wsmostudio.runtime.io.Utils;
50  
51  public class NewPAService  extends Wizard implements INewWizard {
52  
53      protected WizardNewFileCreationPage filePage;
54      protected NewPAServiceIDPage idPage;
55  
56      public void init(IWorkbench workbench, IStructuredSelection selection) {
57          
58          idPage = new NewPAServiceIDPage(selection);
59          initNewFilePage("New Public Administration Service File", selection);
60          filePage.setImageDescriptor(Activator.getSGLogo());
61          setWindowTitle("New Public Administration Service");
62          setDefaultPageImageDescriptor(Activator.getSGLogo());
63      }
64  
65      public void addPages() {
66          addPage(filePage);
67          filePage.setImageDescriptor(Activator.getSGLogo());
68          
69          filePage.setTitle(idPage.getTitle());
70          filePage.setMessage(idPage.getMessage());
71          filePage.setDescription(idPage.getMessage());
72          addPage(idPage);
73      }
74      
75      protected void initNewFilePage(String pageName, IStructuredSelection selection)
76      {
77          filePage = new WizardNewFileCreationPage(pageName, selection) {
78              protected boolean validatePage() {
79                  if (false == super.validatePage()) {
80                      return false;
81                  }
82                  String testName = getFileName();
83                  if (testName.length() == 0) {
84                      return true;
85                  }
86                  if (false == testName.toLowerCase().endsWith(".wsml")) {
87                      testName += ".wsml";
88                  }
89                  
90                  IPath path = super.getContainerFullPath().append(testName);
91                  if (ResourcesPlugin.getWorkspace().getRoot().getFile(path).exists()) {
92                      setErrorMessage("The same name (" + testName + ") already exists!");
93                      return false;
94                  }
95                  return true;
96              }
97          };
98      }
99  
100     public boolean canFinish() {
101         return idPage.isPageComplete();
102     }
103 
104     public boolean isHelpAvailable() {
105         return false;
106     }
107 
108     public boolean needsPreviousAndNextButtons() {
109         return true;
110     }
111 
112     public boolean needsProgressMonitor() {
113         return false;
114     }
115 
116     public boolean performCancel() {
117         return true;
118     }
119 
120     public boolean performFinish() {
121         String input = idPage.getInputIdentifier();
122         if (input == null || input.length() == 0) {
123             return false;
124         }
125         String instOnto = input + "_gea";
126         if (instOnto == null || instOnto.length() == 0) {
127             return false;
128         }
129         String defNS = idPage.getDefaultNamespace();
130         if (defNS == null || defNS.length() == 0) {
131             return false;
132         }
133         
134         if (input.indexOf(":") == -1) {
135             input = defNS + input;
136         }
137         if (instOnto.indexOf(":") == -1) {
138             instOnto = defNS + instOnto;
139         }
140         
141         WsmoFactory wsmoFactory = WSMORuntime.getRuntime().getWsmoFactory();
142         WebService wsmoService = null;
143         Ontology wsmoOntology = null;
144         try {
145             IRI iriRef = wsmoFactory.createIRI(input);
146             wsmoService = wsmoFactory.createWebService(iriRef);
147             wsmoOntology = wsmoFactory.createOntology(wsmoFactory.createIRI(instOnto));
148         }
149         catch(Exception iriEx) {
150             MessageDialog.openError(getShell(), 
151                     "Invalid Identifier",
152                     iriEx.getMessage());
153             return false;
154         }
155         try {
156             wsmoService.setDefaultNamespace(wsmoFactory.createIRI(defNS));
157             wsmoOntology.setDefaultNamespace(wsmoFactory.createIRI(defNS));
158         }
159         catch(Exception nsEx) {
160             MessageDialog.openError(getShell(), 
161                     "Invalid Default Namespace",
162                     nsEx.getMessage());
163             return false;
164         }
165         Utils.updateStudioNFP(wsmoService);
166         Utils.updateStudioNFP(wsmoOntology);
167         if (false == defNS.equals(Constants.GEA_NAMESPACE)) {
168             IRI geaNS = wsmoFactory.createIRI(Constants.GEA_NAMESPACE);
169             wsmoService.addNamespace(wsmoFactory.createNamespace("gea", geaNS));
170             wsmoOntology.addNamespace(wsmoFactory.createNamespace("gea", geaNS));
171         }
172         
173         try {
174             wsmoService.addNFPValue(Constants.NFP_INST_ONTOLOGY, 
175                                wsmoOntology.getIdentifier());
176             wsmoOntology.addNFPValue(Constants.NFP_SERVICE_REF, 
177                                wsmoService.getIdentifier());
178         }
179         catch(InvalidModelException ime) {
180             LogManager.logError(ime);
181         }
182 
183         if (idPage.getReferenceOntology() != null) {
184             wsmoService.addOntology(idPage.getReferenceOntology());
185             wsmoOntology.addOntology(idPage.getReferenceOntology());
186         }
187         
188         IRI capabilityID = wsmoFactory.createIRI(wsmoService.getDefaultNamespace(), 
189                 "Capability_" + idPage.getInputIdentifier());
190         Capability cap = wsmoFactory.createCapability(capabilityID);
191         wsmoService.setCapability(cap);
192         
193         IRI interfaceID = wsmoFactory.createIRI(wsmoService.getDefaultNamespace(), 
194                 "Interface_" + idPage.getInputIdentifier());
195         Interface iface = wsmoFactory.createInterface(interfaceID);
196         wsmoService.addInterface(iface);
197 
198         IFile fileHandle = createWSMOFile(wsmoService);
199         if (fileHandle == null) {
200             return false;
201         }
202 
203         // create the instanceOntology
204         File ontoFile = 
205             new File(fileHandle.getParent().getLocation().toString()+"/gea" + fileHandle.getName());
206         StringBuffer str = new StringBuffer();
207         WSMORuntime.getRuntime().getWsmlSerializer().serialize( new TopEntity[] { wsmoOntology }, str);
208 
209         try {
210             FileOutputStream fileOut = new FileOutputStream(ontoFile, false);
211             fileOut.write(str.toString().getBytes());
212             fileOut.close();
213         }
214         catch(IOException ioe) {
215             LogManager.logError(ioe);
216         }
217         try {
218             fileHandle.getParent().refreshLocal(IResource.DEPTH_ONE, null);
219         }
220         catch(CoreException coreEx) {
221             LogManager.logError(coreEx);
222         }
223      
224         BasicNewResourceWizard.selectAndReveal(fileHandle, 
225                 PlatformUI.getWorkbench().getActiveWorkbenchWindow());
226         return true;
227     }
228     
229     protected IFile createWSMOFile(TopEntity entity) {
230 
231         String newFileName = filePage.getFileName();
232 
233         if (newFileName == null) {
234             return null; // error
235         }
236         
237         if (false == newFileName.toLowerCase().endsWith(".wsml")) {
238             newFileName += ".wsml";
239         }
240         filePage.setFileName(newFileName);
241         IFile file = filePage.createNewFile();
242         // error in creating file
243         if (file == null) {
244             return null;
245         }
246         Serializer wsmlSerializar = WSMORuntime.getRuntime().getWsmlSerializer();
247         StringBuffer str = new StringBuffer();
248         wsmlSerializar.serialize( new TopEntity[] { entity }, str);
249         try {
250             file.setContents(new ByteArrayInputStream(str.toString().getBytes()), true, false, null);
251         }
252         catch(Exception e) {
253             LogManager.logError(e);
254         }
255         return file;
256     }
257 
258 
259     public void dispose() {
260         idPage.dispose();
261         super.dispose();
262     }
263 
264 }
265 
266 /*
267  * $Log: NewPAService.java,v $
268  * Revision 1.4  2007/02/21 14:39:03  alex_simov
269  * instance ontology specification removed from the wizard page
270  *
271  * Revision 1.3  2007/01/23 16:23:34  alex_simov
272  * no message
273  *
274  * Revision 1.2  2007/01/22 17:25:35  alex_simov
275  * no message
276  *
277  * Revision 1.1  2007/01/12 16:30:29  alex_simov
278  * PA Service editor initial version
279  *
280  */