8 Replies Latest reply on May 24, 2013 7:43 AM by NitroHazeDev

    asset metatadata information in a java process step

    sachin1107

      We need to write a java process in a process step of a workflow. We need to do certain actions based on the asset being referred in the workflow.

       

      I am not able to fetch the metadata of the asset(example image). Please share the code for fetching the

      metadata for an asset.

       

      Thanks,

      Sachin

        • 1. Re: asset metatadata information in a java process step
          Geoffroy Schneck Level 2

          I assume you already managed to retrieve the Asset object out of the payload then ?

           

          See the API Javadoc of the Asset class here : http://dev.day.com/docs/en/cq/5-5/javadoc/com/day/cq/dam/api/Asset.html

           

          You can see you have 3 methods to get the Metadata information.

           

          So for example :

           

          Asset asset = resource.adaptTo(Asset.class);

          String assetTitle = asset.getMetadataValue("dc:title");

          • 2. Re: asset metatadata information in a java process step
            sachin1107 Level 1

            Hi,

             

            I am not sure how to get the resource object. I am trying to do it in a workflow process and defining resource as

            ResourceResolver resource;

             

            I believe this is not working as resource is not having any value. How can I get value of resource in a java workflow process.

             

            Thanks in advance.

             

            ~Sachin

            • 3. Re: asset metatadata information in a java process step
              Geoffroy Schneck Level 2

              If you implement the WorkflowProcess class, you should have access to the WorkflowItem and the session. Have a look at (1) .

               

              One way is to use a JCR resource resolver factory to access the Resource and convert it to an Asset. In your class definition :

               

              @Reference(policy = ReferencePolicy.STATIC)

              private JcrResourceResolverFactory jcrResolverFactory;

               

               

              then in your method :

               

              ResourceResolver rr = jcrResolverFactory.getResourceResolver(session)

              String path = item.getWorkflowData().getPayload().toString();

              Resource resource = rr.getResource(path);

              Asset asset = resource.adaptTo(Asset.class);

               

               

              (1) http://dev.day.com/docs/en/cq/current/workflows/wf-extending.html#Defining a Process Step with a Java Class

              • 4. Re: asset metatadata information in a java process step
                sachin1107 Level 1

                Hi,

                 

                This is the code I am using

                 

                public class TestCode  implements WorkflowProcess

                {

                1. private static final Logger log =     LoggerFactory.getLogger(TestCreateAssetWorkflow.class);

                  2. @Reference(policy = ReferencePolicy.STATIC)

                  3.   private JcrResourceResolverFactory jcrResolverFactory;

                4.    public void execute(WorkItem item, WorkflowSession arg1, MetaDataMap arg2)

                                              throws WorkflowException {

                                    // TODO Auto-generated method stub

                5.     try{

                6.       ResourceResolver rr = jcrResolverFactory.getResourceResolver(arg1.getSession());

                7.                 String path = item.getWorkflowData().getPayload().toString();

                8.       Resource resource = rr.getResource(path);

                9.                 Asset asset = resource.adaptTo(Asset.class);

                10.       String assetTitle = asset.getMetadataValue("dc:title");

                     .........

                 

                I am getting  java.lang.NullPointerException error on line 6 where I am trying to get ResourceResolver object. It seems the jcrResolverFactory is not getting intialized.

                The error stack trace prints:

                12.03.2013 15:20:24.488 *INFO* [JobHandler: /etc/workflow/instances/2013-03-12/model_1143994746278437:/content/dam/Test/workingOnComm unication/test5/photos/11331.tif] com.test.dampoc.TestCreateAssetWorkflow Stack Trace::java.lang.NullPointerException

                 

                Please suggest how to proceed.

                 

                Thanks,
                Sachin

                • 5. Re: asset metatadata information in a java process step
                  Sham HC Level 7

                  Hi Sachin,

                   

                      Which version of CQ. IIRC JcrResourceResolverFactory is depricated for 5.4 onwards. Instead use ResourceResolverFactory.

                     

                  Thanks,

                  Sham

                  • 6. Re: asset metatadata information in a java process step
                    sachin1107 Level 1

                    Hi Sham,

                     

                    I have tried using ResourceResolverFactory also, but it has not resolved the issue.

                     

                    When ever I am trying to take the ResourceResolver object from ResourceResolverFactory, I am getting NullPointerException.

                     

                    Below is the code I am using, Please suggest if I am making any mistake.

                     

                    @Component(metatype = true, immediate = true)

                    @Service

                    @Properties({

                    @Property(name = Constants.SERVICE_DESCRIPTION, value = "ForTestingProcesses"),

                    @Property(name = Constants.SERVICE_VENDOR, value = "abc"),

                    @Property(name = "process.label", value = "TestProcess")})

                     

                     

                     

                    public class TestCode  implements WorkflowProcess

                    {

                              private static final Logger log = LoggerFactory.getLogger(TestCreateAssetWorkflow.class);

                              @Reference(policy = ReferencePolicy.STATIC)

                               private ResourceResolverFactory ResolverFactory;

                              ResourceResolver resourceResolver = null;

                              public void execute(WorkItem item, WorkflowSession arg1, MetaDataMap arg2)

                                                  throws WorkflowException {

                              try{

                                        String path=item.getWorkflowData().getPayload().toString();

                                        log.info("TestCode Path::"+path);

                                        resourceResolver = ResolverFactory.getAdministrativeResourceResolver(arg2);

                                        log.info("Path2::"+path);

                                        Resource res=resourceResolver.getResource(path);

                                        log.info("Path3::"+path);

                                        Asset damAsset = DamUtil.resolveToAsset(res);

                                        log.info("damasset::"+damAsset);

                      }catch(Exception e){

                                                  log.info("Stack Trace::"+e);

                                                  }

                                        }

                    }

                     

                    if I include the line  @Reference(policy = ReferencePolicy.STATIC), It is giving error that the process is not found. If I am excluding the mentioned line, I am getting NullPointer exception.

                     

                    Please suggest.

                     

                    Thanks,
                    Sachin

                    • 7. Re: asset metatadata information in a java process step
                      KaushikNitin

                      Instead of exetending WorkflowProcess try AbstractAssetWorkflowProcess .

                       

                      and in execute method try the folllowing code

                       

                      getAssetFromPayload(workItem, workflowSession.getSession());

                      it will return you the current asset in payload .

                       

                      There are various methods in AbstractAssetWorkflowProcess which you can use for asset processing.

                       

                      Thanks . Hope It Will work for you.

                      • 8. Re: asset metatadata information in a java process step
                        NitroHazeDev Level 1

                        Hey Sachin,

                         

                        Were you able to extract the metadata of an asset eg: image uploaded? I am stuck, need some advice on this.

                         

                        Regards,

                        NZ