April 7, 2020
Estimated Post Reading Time ~

Projects API in AEM



AEM included a concept of ‘Project’ along with its Touch UI navigation. Projects allow you to have a collection of all the related resources for a project together. Resources include Sites, workflows, Assets, Launches, etc. Read AEM-Projects to know more about Projects.




With this console, you can access and take action on your projects. You can create the project with the predefined project templates and associate the related resources. Similarly, you will also be able to delete a project or a resource respectively. Check here to know about managing projects.



Projects API 
  • There is a Projects API available to create the same programmatically
  • Package com.adobe.cq.projects.api
  • Project is an interface which holds all the properties of a project like a title, description, member, etc
  • ProjectManager is an interface which has the methods to create, delete projects
Example 
Now, let us see how to use those API with the below example. I have tried to use both Projects and ProjectManager interfaces showing the basic functionality of how to
  • Create a Project
  • Retrieve a Project and
  • Delete a Project
Create 
If you quickly want to check how it works, you can download the example project from github or

Create a new AEM maven multi-module project. Add the below two classes in the bundle

Service 
package org.test.adobeaemclub.core.service;

import org.apache.sling.api.resource.ResourceResolver; 
import com.adobe.cq.projects.api.Project;

public interface ProjectService { 
 public void createProject();
 public void deleteProject(); 
 public Project getProject(ResourceResolver rr);
 }

ServiceImpl
 /** 
 * 
 */ 

package org.test.adobeaemclub.core.service.impl; 
 import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.Iterator; 
import java.util.List; 
import java.util.Map; 
import org.apache.felix.scr.annotations.Activate; 
import org.apache.felix.scr.annotations.Component; 
import org.apache.felix.scr.annotations.Reference; 
import org.apache.felix.scr.annotations.Service; 
import org.apache.sling.api.resource.ResourceResolver; 
import org.apache.sling.api.resource.ResourceResolverFactory; 
import org.slf4j.Logger; import org.slf4j.LoggerFactory; 
import org.test.adobeaemclub.core.service.ProjectService; 
 import com.adobe.cq.projects.api.Project; 
import com.adobe.cq.projects.api.ProjectFilter; 
import com.adobe.cq.projects.api.ProjectManager;

 /** 
 * @author Loki 
 * 
 */ 

@Component(immediate=true) @Service public class ProjectServiceImpl implements ProjectService { 

 /* (non-Javadoc) 
 * @see org.test.adobeaemclub.core.service.ProjectService#createProject() 
 * 
 */ 

 private final Logger logger = LoggerFactory.getLogger(getClass()); 
 @Reference 
 ResourceResolverFactory rrfactory; 
 HashMap<String, Object> map = new HashMap<String, Object>(); ResourceResolver rr = null; 
 
@Activate 
 protected void activate(final Map<String, Object> config) { map.put(ResourceResolverFactory.SUBSERVICE, "writeService"); 
 deleteProject(); 
 createProject(); 
 } 

 @Override 
 public void createProject() { 
 try { 
 rr = rrfactory.getServiceResourceResolver(map); 
 ProjectManager pm = rr.adaptTo(ProjectManager.class); 
 Project project = pm.createProject("sampleproject", "Sample Project", "//libs//cq//core//content//projects//templates//default"); project.setDescription("This Project is created programmatically"); 
 rr.commit(); logger.info("Project Created Successfully....."); 
 logger.info("Project Title : " + project.getTitle());
 logger.info("Project Description : " + project.getDescription()); 
 } 
catch (Exception e) { 
 e.printStackTrace(); 
 }finally{ 
 if(rr.isLive()) rr.close(); 
 } 
 } 
 /* (non-Javadoc) * @see org.test.adobeaemclub.core.service.ProjectService#deleteProject() */ 

 @Override 
 public void deleteProject() { 
 try { 
 rr = rrfactory.getServiceResourceResolver(map); 
 ProjectManager pm = rr.adaptTo(ProjectManager.class); 
 Project delProj = getProject(rr); pm.deleteProject(delProj); 
 rr.commit(); logger.info("Project Deleted Successfully ... "); 
 } catch (Exception e) { 
 e.printStackTrace(); 
 }finally{
 if(rr.isLive()) rr.close(); 
 } 
 } 

 /* (non-Javadoc) * @see org.test.adobeaemclub.core.service.ProjectService#getProjects() */ 

 @Override 
 public Project getProject(ResourceResolver rr) { 
 Project delProj = null; 
 ProjectManager pm = rr.adaptTo(ProjectManager.class); 
 ProjectFilter filters = new ProjectFilter(); 
 List<String> templates = new ArrayList<String>(); templates.add("/libs/cq/core/content/projects/templates/default"); filters.setProjectTemplates(templates); 

 //filters.setActive(true); To get all the active projects Iterator<Project> 
projects = pm.getProjects(filters, 0, 10); 
 while (projects.hasNext()) 
 Project project = projects.next();
 if(project.getTitle().equals("Sample Project")){
 delProj = project; logger.info("Found a Project... ");
 } 
 } 
 return delProj; 
 } 
 }

Note: I have used a subService called ‘writeService’ to get the resource resolver. Make sure you create the same or use the existing subservice if you have already created it.

Build and Install 
Once you have a project created, build and install the bundle using the mvn command
Mvn clean install –PautoInstallBundle

Test 
You can check the Projects console and see if the new project created.



You can also check the logs. The first time when you install the bundle it creates the project.


When you install it again, it retrieves and deletes the existing project and create the project again.


Hope this example helps you to know how to use the Projects APIs


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.