Expand my Community achievements bar.

SOLVED

ACS AEM commons in JAVA

Avatar

Level 4

Hello,

I have requirement on archiving the assets, where i need to send a mail to the folder owners before one month of actual expiry date. So we are planning to take the folder owners mail id from the ACS AEM commons generic list.

I am looking for the help how we can access the generic list directly on backend without adding the ACS AEM commons on the component dialog as a datasource. If anyone knows the way to fetch the list value in the backend, kindly let me know here. Thanks much in advance.

 

 

Regards,

Bhavani Bharanidharan

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @BhavaniBharani,

import com.adobe.acs.commons.genericlists.GenericList;
import com.adobe.acs.commons.genericlists.GenericListManager;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import java.util.List;

@Component(
        immediate = true,
        service = { MyService.class },
        property = {
                Constants.SERVICE_DESCRIPTION + "=My Service",
                "sling.servlet.methods=GET",
                "sling.servlet.paths=/bin/my-service"
        }
)
public class MyService {

    @Reference
    private GenericListManager genericListManager;

    public void myMethod(SlingHttpServletRequest request) {
        ResourceResolver resolver = request.getResourceResolver();

        // Specify the path to your Generic List
        String listPath = "/etc/acs-commons/lists/my-generic-list";

        GenericList genericList = genericListManager.getGenericList(resolver, listPath);

        if (genericList != null) {
            List<ValueMap> entries = genericList.getEntries();
            
            // Now, you can iterate through the entries and retrieve the required information
            for (ValueMap entry : entries) {
                String value = entry.get("value", String.class);
                // Process the value as needed
            }
        }
    }
}

This example assumes that you have the ACS AEM Commons Generic List located at "/etc/acs-commons/lists/my-generic-list". Adjust the listPath accordingly based on your configuration. 

Also, Generic Lists are not accessible to everyone by default on AEM as a Cloud Service Publish service. To make generic lists available on AEM Publish, set the appropriate jcr:read ACLs on either the /etc/acs-commons/list node, on specific list pages under /etc/acs-commons/lists. You can find the reference here

https://adobe-consulting-services.github.io/acs-aem-commons/features/generic-lists/index.html

Note: Make sure that the appropriate version of ACS AEM Commons is installed and the necessary dependencies are available in your project.

Thanks

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

Hi @BhavaniBharani,

import com.adobe.acs.commons.genericlists.GenericList;
import com.adobe.acs.commons.genericlists.GenericListManager;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import java.util.List;

@Component(
        immediate = true,
        service = { MyService.class },
        property = {
                Constants.SERVICE_DESCRIPTION + "=My Service",
                "sling.servlet.methods=GET",
                "sling.servlet.paths=/bin/my-service"
        }
)
public class MyService {

    @Reference
    private GenericListManager genericListManager;

    public void myMethod(SlingHttpServletRequest request) {
        ResourceResolver resolver = request.getResourceResolver();

        // Specify the path to your Generic List
        String listPath = "/etc/acs-commons/lists/my-generic-list";

        GenericList genericList = genericListManager.getGenericList(resolver, listPath);

        if (genericList != null) {
            List<ValueMap> entries = genericList.getEntries();
            
            // Now, you can iterate through the entries and retrieve the required information
            for (ValueMap entry : entries) {
                String value = entry.get("value", String.class);
                // Process the value as needed
            }
        }
    }
}

This example assumes that you have the ACS AEM Commons Generic List located at "/etc/acs-commons/lists/my-generic-list". Adjust the listPath accordingly based on your configuration. 

Also, Generic Lists are not accessible to everyone by default on AEM as a Cloud Service Publish service. To make generic lists available on AEM Publish, set the appropriate jcr:read ACLs on either the /etc/acs-commons/list node, on specific list pages under /etc/acs-commons/lists. You can find the reference here

https://adobe-consulting-services.github.io/acs-aem-commons/features/generic-lists/index.html

Note: Make sure that the appropriate version of ACS AEM Commons is installed and the necessary dependencies are available in your project.

Thanks

Avatar

Community Advisor

Hi @BhavaniBharani 
Sling API to query the repository. Below is a sample Java code

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.osgi.service.component.annotations.Reference;
import com.adobe.granite.security.user.UserManager;
import com.adobe.granite.security.user.User;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class ACSCommonsUtil {

    @Reference
    private ResourceResolverFactory resolverFactory;

    // Method to get the email addresses of folder owners from ACS Commons Generic List
    public List<String> getFolderOwnersEmails(String folderPath) {
        List<String> emails = new ArrayList<>();

        try (ResourceResolver resolver = resolverFactory.getServiceResourceResolver(null)) {
            // Path to ACS Commons Generic List
            String genericListPath = "/etc/acs-commons/lists/your-generic-list-name";

            // Construct the path to the Generic List entry for the given folderPath
            String genericListEntryPath = genericListPath + "/" + folderPath.replace("/", "_");

            // Get the Resource for the Generic List entry
            Resource genericListEntryResource = resolver.getResource(genericListEntryPath);

            if (genericListEntryResource != null) {
                // Assuming the property storing email addresses is named "emails"
                emails = genericListEntryResource.getValueMap().get("emails", new ArrayList<String>().getClass());
            }

        } catch (Exception e) {
            // Handle exceptions
        }

        return emails;
    }

    // Method to get the folder owner's email from the user manager
    public String getFolderOwnerEmail(String folderPath) {
        try (ResourceResolver resolver = resolverFactory.getServiceResourceResolver(null)) {
            UserManager userManager = resolver.adaptTo(UserManager.class);

            // Assuming the folder owner's user ID is stored in the folder's "owner" property
            String ownerId = resolver.getResource(folderPath).getValueMap().get("owner", String.class);

            if (ownerId != null) {
                User user = (User) userManager.getAuthorizable(ownerId);
                return user.getProperty("profile/email", String.class);
            }

        } catch (Exception e) {
            // Handle exceptions
        }

        return null;
    }
}

 This example assumes that you have ACS AEM Commons installed, and there is a Generic List containing email addresses for each folder. It also assumes that the user ID of the folder owner is stored in the "owner" property of the folder.