Search This Blog

About Me

Highly experienced in implementation and customization of Alfresco. More than 11 years of experience working on Alfresco, versions ranging from 2.x to 5.x. Successfully processed innovative and tailored CMS solutions to meet ever-changing business requirements for multiple clients. Role Includes : Experience in Alfresco Upgrades, SOLR migration, Proposals, Team Handling, Delivery, Content Migration, POC's etc.. Extensive experience in client co ordination, working on offshore-onshore model and project take over. Technical Expertise Includes : Java, JSP, Servlet, JSF, JavaScript, AJAX, Spring, SQL, Hibernate, Web Services(REST). Specialities Includes : Content Migration, Product Customization, Business CMS Solutions, Workflow, Project Transition.

Wednesday, March 10, 2010

Different approaches of Accessing Alfresco Services

As alfresco services are the only tools we can use for making any changes inside alfresco. Here i am writing some of the approaches for getting them.

There are three approaches i have used till now for accessing the alfresco services ( Searchservice, NodeService etc) in a class.

1. Accessing through application context.

As mentioned in alfresco documentation and in examples too, we can get the application context object from alfresco utility class 'ApplicationContextHelper'

// Do not forget to include alfresco jars into your project
ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();

Now from application context, get the service registry bean having all the registered services for alfresco, like this

ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);

And from this service registry object you can get any alfresco services, for e.g like this

SearchService searchService = serviceRegistry.getSearchService();
NodeService nodeService = serviceRegistry.getNodeService();


OR

You can get it by following these steps also

ApplicationContext appContext = new ClassPathXmlApplicationContext("alfresco/application-context.xml");

ServiceRegistry registry = (ServiceRegistry)appContext.getBean(ServiceRegistry.SERVICE_REGISTRY);


2. Getting services from repository directly

SearchService searchService = Repository.getServiceRegistry(FacesContext.getCurrentInstance()).getSearchService();

3. Setting services into properties in faces context and accessing it directly just by declaring getters and setters

a. Register your bean into faces-config-custom.xml and set the services you want to use in your class between <manage-property> tag with property-name and value, for example, below i have declared search service to be used into the bean i am declaring


<managed-bean>
<description>{Bean Description You want to Provide}</description>
<managed-bean-name>{Bean Name}</managed-bean-name>
<managed-bean-class>
{Package Name.Bean Name}
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>searchService</property-name>
<value>#{SearchService}</value>
</managed-property>       
</managed-bean>


b. Declare and write the getters and setters for the properties you have declared and want to use in your class

public class TestClass
{
private SearchService searchService;
/** Your code */
public SearchService getSearchService() {
return searchService;
}
public void setSearchService(SearchService searchService) {
this.searchService = searchService;
}
}