2 Replies Latest reply on Sep 24, 2012 9:03 PM by Tihomir Surdilovic

    Link between org.drools.definition.process.Process and asset in Guvnor

    Alessandro Dionisi Newbie

      Hi all,

      in my application, I use a KnowledgeAgent that scans a Guvnor ChangeSet in order to see the latest modification on the process definitions.

      I can list the process definitions in the kbase using: ksession.getKnowledgeBase().getProcesses(). This call returns a list of org.drools.definition.process.Process objects that has the following attributes: id, name, version, package name, type, metadata. This is an example of table displaying that data:

      Screenshot_1.png

      Now I want to add an edit feature, that opens directly the Designer in the application context, not Guvnor, using the URL: http://localhost:8080/drools-Guvnor/org.drools.guvnor.Guvnor/standaloneEditorServlet?assetsUUIDs=" + processUuid + "&client=designer";

       

      The problem is that I can't find a link from the Process object to the UUID of the resource in Guvnor. How can I find the UUID starting from org.drools.definition.process.Process object?


      Thank you for an answer,

      Alessandro

        • 1. Re: Link between org.drools.definition.process.Process and asset in Guvnor
          Alessandro Dionisi Newbie

          I've written this function that using the REST API retrieves the process UUID. It returns the first asset (BPMN file) that has a matching process ID in the definition.

          Do you think it's a right solution?

           

           

          public String getProcessUUID(Process p) {

           

                              try {

                                        URL restUrl = new URL(url + "rest/packages/");

                                        HttpURLConnection connection = (HttpURLConnection) restUrl.openConnection();

                                        connection.setRequestProperty("Authorization", "Basic " + new Base64().encodeToString(((username + ":" + password).getBytes())));

                                        connection.setRequestMethod("GET");

                                        connection.setRequestProperty("Accept", MediaType.APPLICATION_ATOM_XML);

                                        connection.connect();

                                        assertEquals(200, connection.getResponseCode());

                                        assertEquals(MediaType.APPLICATION_ATOM_XML, connection.getContentType());

           

           

                                        // Parse response

                                        InputStream in = connection.getInputStream();

                                        Document<Feed> doc = abdera.getParser().parse(in);

                                        Feed feed = doc.getRoot();

                                        List<Entry> entries = feed.getEntries();

                                        Iterator<Entry> it = entries.iterator();

           

           

                                        while (it.hasNext()) {

                                                  Entry entry = it.next();

           

           

                                                  // Match package

                                                  if (p.getPackageName().equals(entry.getTitle())) {

           

           

                                                            // Get the assets

                                                            restUrl = new URL(url + "rest/packages/" + p.getPackageName() + "/assets");

                                                            HttpURLConnection assetConnection = (HttpURLConnection) restUrl.openConnection();

                                                            assetConnection.setRequestProperty("Authorization", "Basic " + new Base64().encodeToString(((username + ":" + password).getBytes())));

                                                            assetConnection.setRequestMethod("GET");

                                                            assetConnection.setRequestProperty("Accept", MediaType.APPLICATION_ATOM_XML);

                                                            assetConnection.connect();

                                                            assertEquals(200, assetConnection.getResponseCode());

                                                            assertEquals(MediaType.APPLICATION_ATOM_XML, assetConnection.getContentType());

           

           

                                                            // Parse response

                                                            InputStream in2 = assetConnection.getInputStream();

                                                            Document<Feed> assets = abdera.getParser().parse(in2);

                                                            Feed assetFeed = assets.getRoot();

                                                            List<Entry> assetsEntries = assetFeed.getEntries();

           

           

                                                            for (Entry atomEntry : assetsEntries) {

           

           

                                                                      ExtensibleElement metadataExtension = atomEntry.getExtension(Translator.METADATA);

                                                                      ExtensibleElement formatExtension = metadataExtension.getExtension(Translator.FORMAT);

                                                                      ExtensibleElement uuidExtension = metadataExtension.getExtension(Translator.UUID);

                                                                      String uuid = uuidExtension.getSimpleExtension(Translator.VALUE);

           

           

                                                                      if (formatExtension.getSimpleExtension(Translator.VALUE).contains("bpmn")) {

                                                                                // Check if the bpmn file contains the specified process ID

                                                                                SemanticModules modules = new SemanticModules();

                                                                                modules.addSemanticModule(new BPMNSemanticModule());

                                                                                modules.addSemanticModule(new BPMNDISemanticModule());

                                                                                XmlProcessReader processReader = new XmlProcessReader(modules, getClass().getClassLoader());

                                                                                processReader.read(atomEntry.getContentStream());

                                                                                // The bpmn file matches the process ID, return the UUID of the asset

                                                                                if (processReader.getProcess().get(0).getId().equals(p.getId()))

                                                                                          return uuid;

                                                                      }

                                                            }

                                                  }

                                        }

                              } catch (Exception e) {

                                        e.printStackTrace();

                              }

                              return null;

                    }