Thursday, February 15, 2018

Importing an xml file in to AEM


You can write a Java service class (i.e. OSGI service) which reads your XML file data and using JCR API's  it can create corresponding nodes (with data & properties) inside CRX repository and persist it. 

Once u have all the data u can read this anywhere on AEM pages (i.e.websites, widgets etc).  Use  XSLT to convert from one xml format to other.

Below is sample automated imported class for importing xml and convert to AEM xml file.

===================================================================
@Service(value = Importer.class)
@Component
@Property(name = "importer.scheme", value = "importedData", propertyPrivate =true)

public class DataImporter implements Importer {
private final String SOURCE_URL = "http://someserver/data.xml";
private static final Logger LOGGER = LoggerFactory.getLogger(DealerDataImporter.class);

@Override
public void importData(String s, String s2, Resource resource) throws ImportException {

    try {

        URL url = new URL(SOURCE_URL);
        URLConnection connection = url.openConnection();
        Document doc = parseXML(connection.getInputStream());
        NodeList Nodes = doc.getElementsByTagName("retailer");
        for (int i = 0; i < Nodes.getLength(); i++) {
            Element element = (Element) Nodes.item(i);
            String id = element.getElementsByTagName("id").item(0).getTextContent();
            String name = element.getElementsByTagName("display_name").item(0).getTextContent();
            String about = element.getElementsByTagName("about").item(0).getTextContent();
            writeToRepository(id, name, about, resource);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void writeToRepository(String id, String name, String about, Resource resource) {

    try {
        javax.jcr.Node parentNode = resource.adaptTo(javax.jcr.Node.class);

        //Say we want this to be a Page node (use NameConstants.NT_PAGE = cq:Page)
        //all of node types can be created this way
        javax.jcr.Node pageNode = JcrUtil.createPath(parentNode.getPath() + "/" + name, NameConstants.NT_PAGE, parentNode.getSession());
        //Page nodes need jcr:content node to hold all teh relevant properties (NameConstants.NN_CONTENT = "jcr:content")
        javax.jcr.Node contentNode = JcrUtil.createPath(pageNode.getPath() + "/" + NameConstants.NN_CONTENT, "cq:PageContent", parentNode.getSession());
        //set some properties
        contentNode.setProperty("about", about);
        contentNode.setProperty("id", id);
        //save session
        parentNode.getSession().save();

    } catch (Exception e1) {
        e1.printStackTrace();
    } 
}
private Document parseXML(InputStream stream){...}

}
=================================================================
After above code execution follow below steps for further processing :


* In CRXde under /etc/importers/polling add new node of type sling:Folder and add some properties:
a) target [String] this is a path to a repository resource this resource will be parsed to your Importer Class.
b) source [String] this is if you're looking to import from multiple xml files IMPORTANT the value needs to start importedData (as a property in the class that is where they link) followed by :SOME_VALIABLE this is a s2 varaible in the above example.
c) interval[Long] how often to run Importer
d) add an mixin jcr:mixinTypes type cq:PollConfig

Thats it.

Sunday, February 11, 2018

Migration of content fromTeam Site to CQ5/ AEM


Before we migrate need to consider below points.

  • How to migrate existing Team site content store to AEM.
  • Around "n" assets( which includes images, docs, pdf, video and audio files).How we can upload assets from Team site to AEM with metadata.
  • Migrate existing users & groups to AEM.
  • Implement custom login authentication.
Refer below link for more information :  https://forums.adobe.com/thread/2328370

Another  best approach for migration is use of XML files conversion.

Follow below url for more information :
http://cq5aemtutorials.blogspot.com/2018/02/importing-xml-file-in-to-aem.html