[rules-users] Changing asset metadata properties in Guvnor

Jervis Liu jliu at redhat.com
Thu Mar 28 03:48:31 EDT 2013


On 2013/3/26 20:56, morfeus929 wrote:
> My code is :
>
> URL baseURL = new
> URL("http://localhost:8080/guvnor-5.5.0.Final-tomcat-6.0/");
> 		URL url = new URL(baseURL, "rest/packages/mortgages/assets/bobik");
> 		HttpURLConnection connection = (HttpURLConnection)url.openConnection();
>          connection.setRequestProperty("Authorization",
>                  "Basic " + new Base64().encodeToString((
> "admin:admin".getBytes() )));
>          connection.setRequestMethod("GET");
>          connection.setRequestProperty("Accept", MediaType.APPLICATION_XML);
>          connection.connect();
>          assertEquals (200, connection.getResponseCode());
>
>          BufferedReader br = new BufferedReader(new
> InputStreamReader(connection.getInputStream()));
>          JAXBContext context = JAXBContext.newInstance(Asset.class);
>          Unmarshaller un = context.createUnmarshaller();
>          Asset a = (Asset) un.unmarshal(br);
>         
>          a.setDescription("An updated description HH");
>          
>          a.getMetadata().setDisabled(false);
>          a.getMetadata().setVersionNumber(2);
>
>          System.out.println(a.getMetadata().isDisabled());
>          System.out.println(a.getMetadata().getVersionNumber());
>
>          
>          connection.disconnect();
>
>          HttpURLConnection conn2 = (HttpURLConnection)url.openConnection();
>          conn2.setRequestProperty("Authorization",
>                  "Basic " + new Base64().encodeToString((
> "admin:admin".getBytes() )));
>          Marshaller ma = context.createMarshaller();
>          conn2.setRequestMethod("PUT");
>          conn2.setRequestProperty("Content-Type", MediaType.APPLICATION_XML);
>          conn2.setRequestProperty("Content-Length",
> Integer.toString(a.toString().getBytes().length));
>          conn2.setUseCaches (false);
>          conn2.setDoInput(true);
>          conn2.setDoOutput(true);
>          ma.marshal(a, conn2.getOutputStream());
>          assertEquals (200, connection.getResponseCode());
>          conn2.disconnect();
so you are trying to update asset metadata "disabled" and 
"versionNumber" through REST API?

I am afraid "versionNumber" is a read-only property, it is controlled by 
JCR version system, it is changed when you check in an asset. You dont 
set this version number directly.

On the other updating "disabled " through REST api is not supported in 
5.5. Please raise a jira if you wish this is added in the future releases.

BTW,  you can take a look into source, then you can understand which 
metadata property is supported and which is not:
Below is code snippet copied from 
https://github.com/droolsjbpm/guvnor/blob/5.5.x/guvnor-webapp-core/src/main/java/org/drools/guvnor/server/jaxrs/PackageResource.java
     @PUT
     @Path("{packageName}/assets/{assetName}")
     @Consumes(MediaType.APPLICATION_ATOM_XML)
     public void updateAssetFromAtom(@PathParam("packageName") String 
packageName, @PathParam("assetName") String assetName, Entry assetEntry) {
         try {
             AtomAssetMetadata assetMetadata = 
assetEntry.getAnyOtherJAXBObject(AtomAssetMetadata.class);

             //Throws RulesRepositoryException if the package or asset 
does not exist
             AssetItem ai = 
rulesRepository.loadModule(packageName).loadAsset(assetName);
             //Update asset
             ai.checkout();
             ai.updateTitle(assetEntry.getTitle());
             ai.updateDescription(assetEntry.getSummary());
             if (assetMetadata.getFormat() != null) {
                 ai.updateFormat(assetMetadata.getFormat());
             }

             //REVISIT: What if the client really wants to set content 
to ""?
             if (assetEntry.getContent().getText() != null && 
!"".equals(assetEntry.getContent().getText())) {
                 ai.updateContent(assetEntry.getContent().getText());
             }
             if (assetMetadata.getCategories() != null && 
assetMetadata.getCategories().length > 0) {
ai.updateCategoryList(assetMetadata.getCategories());
             }
             if (assetMetadata.getState() != null) {
                 ai.updateState(assetMetadata.getState());
             }
             ai.updateValid(assetValidator.validate(ai));
             if (AssetFormats.affectsBinaryUpToDate(ai.getFormat())) {
                 ModuleItem pkg = ai.getModule();
                 pkg.updateBinaryUpToDate(false);
                 RuleBaseCache.getInstance().remove(pkg.getUUID());
             }
             ai.checkin("Check-in (summary): " + assetEntry.getSummary());
             rulesRepository.save();
         } catch (JAXBException e) {
             throw new WebApplicationException(e);
         } catch (RuntimeException e) {
             throw new WebApplicationException(e);
         }
     }

     @PUT
     @Path("{packageName}/assets/{assetName}")
     @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
     public void updateAssetFromJAXB(
             @PathParam("packageName") String packageName,
             @PathParam("assetName") String assetName, Asset asset) {
         try {
             //Throws RulesRepositoryException if the package or asset 
does not exist
             AssetItem ai = 
rulesRepository.loadModule(packageName).loadAsset(assetName);
             /* Update asset */
             ai.checkout();
             ai.updateTitle(asset.getTitle());
             ai.updateDescription(asset.getDescription());
             ai.updateValid(assetValidator.validate(ai));
             if (AssetFormats.affectsBinaryUpToDate(ai.getFormat())) {
                 ModuleItem pkg = ai.getModule();
                 pkg.updateBinaryUpToDate(false);
                 RuleBaseCache.getInstance().remove(pkg.getUUID());
             }
             ai.checkin(asset.getMetadata().getCheckInComment());
             rulesRepository.save();
         } catch (RuntimeException e) {
             throw new WebApplicationException(e);
         }
     }

As you can see, only title, description, checkinComment are supported if 
you use application/xml content type.  if you use Atom, there are more 
supported attributes, include: title, description, checkinComment, 
format, state, categories.

Cheers,
Jervis
>
> The problem i have is metadata of the rule does'nt get changed. I tried
> almost everything. Description of the rule is changed, published date is
> changed but metadata not. Why is that? What do i do wrong?
>
>
>
> --
> View this message in context: http://drools.46999.n3.nabble.com/rules-users-Changing-asset-metadata-properties-in-Guvnor-tp4023019p4023046.html
> Sent from the Drools: User forum mailing list archive at Nabble.com.
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users



More information about the rules-users mailing list