1

I am writing a deployment extension using java code in SDL Tridion.

My requirement is to get the component fields(in extension) and i need to send those field values to some other API.

Can any one please help me with the Java code to get the component fields data.

1
  • Can you be more specific? where exactly do you need the component fields? in the extension or in an application? Commented Aug 24, 2017 at 10:27

2 Answers 2

1

You will not have access to the components fields in a deployer extension. What you work with is the published content which may be a page markup resulting from a CM side page template or a component presentation output for instance. You can enrich / modify the output before it is finally deployed to the file system or Content Delivery broker database. You can also use some of the output to export to another system during the deployment process. For instance deployer extensions are used to invalidate cache or update a search engine index.

Accessing the component fields on the Content Delivery side is usually done in your webapp itself, with the end goal of displaying the component fields data onto a web page, using SDL Web's Content Delivery APIs to query that content. DXA or DD4T can help you with that by making the component data model available from your MVC views.

It would be nice to explain what you are trying to achieve in more details, so that we can confirm that a deployer extension is the right approach for your use case.

0

I need the component fields in the extension only (not in application). After that I have to send the component data to some other API.

Below is my sample java code. I am not sure how to get component fields (title, metadata and some fields).

package com.tridion.kd.extensions;

import java.util.Iterator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tridion.configuration.Configuration;
import com.tridion.configuration.ConfigurationException;

import com.tridion.transport.transportpackage.*;
import com.tridion.ItemTypes;
import com.tridion.configuration.*;
import com.tridion.deployer.ProcessingException;
import com.tridion.deployer.Module;
import com.tridion.deployer.Processor;
import com.tridion.util.TCMURI;

public class MyCustomModule extends Module 
{
                private static Logger log = LoggerFactory.getLogger(MyCustomModule.class);
                MetaDataFile compPresMetadata = null;
                MetaDataFile componentMeta = null;
                Configuration config = null;
                MetaDataFile pageMetadata = null;

                @SuppressWarnings("deprecation")
                public MyCustomModule(Configuration config, Processor processor) throws ConfigurationException 
                {
                                super(config, processor);
                                this.config = config;
                                log.info("CacheFlusher Value");

                }

                // This method is called once for each TransportPackage that is deployed.
                public void process(TransportPackage data) 
                {
                    try 
                    {
                            ProcessorInstructions instructions = data.getProcessorInstructions();
                            Iterator iterator = instructions.getArguments();
                            do 
                            {
                                    if (!iterator.hasNext())
                                                    break;
                                    Object argument = iterator.next();
                                    if (argument instanceof Publication) 
                                    {
                                            Publication pubKey = (Publication) argument;
                                            log.debug("PublicationName from Response::" + pubKey.getTitle());
                                            log.info("instructions.getMetaData(Page) " + instructions.getMetaData("Pages").getName());
                                            MetaData compPresMetadataInfo = instructions.getMetaData("ComponentPresentations");
                                            MetaData compMetadataInfo = instructions.getMetaData("Components");
                                            MetaData pagesMetadataInfo = instructions.getMetaData("Pages");
                                            compPresMetadata = data.getMetaData("ComponentPresentations", compPresMetadataInfo.getName());
                                            pageMetadata = data.getMetaData("Pages", pagesMetadataInfo.getName());
                                            log.info("compPresMetadataInfo.getName() " + compPresMetadataInfo.getName());
                                            componentMeta = data.getMetaData("Components", compMetadataInfo.getName());

                                            log.info("compMetadataInfo.getName() " + compMetadataInfo.getName());

                                            if(pubKey.getTitle() == "040 Content Library EN")
                                            {
                                                Section section = null;
                                                Iterator<Section> Sections = instructions.getSections();
                                                for (; Sections.hasNext(); processSection(section, data)) 
                                                {
                                                    section = Sections.next();
                                                }
                                            }






                                    }

                            } while (true);

                    } 
                    catch (Exception e) 
                    {
                        log.error("Could not get custom configuration", e);
                    }
                }

                protected void processSection(Section section, TransportPackage data) 
                {
                    if (log.isDebugEnabled()) 
                    {
                        log.debug("        Section " + section.getName());
                    }
                    Iterator iterator = section.getFileItems();
                    Object item;

                    for (; iterator.hasNext(); processItem(item, section)) 
                    {
                        item = iterator.next();
                    }
                }

                @SuppressWarnings("deprecation")
                protected void processItem(Object obj, Section section) 
                {
                    if (obj instanceof PageKey) 
                    {
                        try 
                        {                                                        
                            PageKey pageKey = (PageKey) obj;
                            Page page = (Page) pageMetadata.getMetaData(pageKey);
                            TCMURI pageTCMID = page.getId();
                            //invokeCacheRefreshByContentId(pageTCMID.toString());
                            log.debug("page.getId().getItemId() " + page.getId().getItemId());
                            log.debug("page.getId().getItemId() " + page.getId());
                        } 
                        catch (Exception e) 
                        {
                            if (log.isErrorEnabled()) 
                            {
                                log.error("Error occured in processing the Page Item" + e.getMessage());
                            }
                        }
                    }
                    else if (obj instanceof ComponentPresentationKey) 
                    {
                        if (log.isDebugEnabled()) 
                        {
                            log.debug("Entered ComponentPresentation");
                        }
                        try 
                        {                                                        
                            ComponentPresentationKey cpKey = (ComponentPresentationKey) obj;
                            log.debug("cpKey" + cpKey);
                            ComponentPresentation componentPresentation = (ComponentPresentation) compPresMetadata.getMetaData(cpKey);
                            log.debug("compPresMetadata.getMetaData(cpKey)" + compPresMetadata.getMetaData(cpKey));

                            if (log.isDebugEnabled()) 
                            {
                                log.debug("componentPresentationgetTemplateKey getTitle()"
                                                                            + componentPresentation.getTemplateKey().getTitle());
                            }                                              
                            TCMURI compTCMID = componentPresentation.getComponentKey().getId();

                            //log.debug(componentPresentation.getComponentKey().getTitle());                    
                            String parsedComponentId = compTCMID.toString();

                            if (log.isDebugEnabled()) 
                            {
                                log.debug("RefreshCache component id::" + parsedComponentId);
                            }


                            if(section.getSchema(parsedComponentId).getTitle() == "Offers")
                            {

                            }

                        } 
                        catch (Exception e) 
                        {
                            if (log.isErrorEnabled()) 
                            {
                                log.error("Error occured in processing the Component Item" + e.getMessage());
                            }
                        }
                    }
                    else if(obj instanceof ComponentKey)
                    {
                        try
                        {
                            ComponentKey compKey = (ComponentKey) obj;

                            ComponentPresentation componentPresentation = (ComponentPresentation) compPresMetadata.getMetaData(compKey);

                            log.info("compMetadataInfo.getName() " + componentPresentation.getComponentKey().getId());

                        }
                        catch(Exception e)
                        {

                        }
                    }

                }                        

}
1
  • I think that this should be an addition to the original question, rather than an answer. Commented Aug 30, 2017 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.