Foros de discusión

Not able to add User in a Instance After Creating a Company via Code.

thumbnail
Parth Ghiya, modificado hace 7 años.

Not able to add User in a Instance After Creating a Company via Code.

Junior Member Mensajes: 35 Fecha de incorporación: 2/07/13 Mensajes recientes
Hello All,

My requirement is to create a new instance on new registration.

I do the following steps.
1. Create Company Via Code & Return the companyId so i can use it in later method for adding new User.
2. After seeing Liferay Code : I found i need to call method for initCompany [PortalInstances.initCompany]
as it is in portal-impl i call that method via portalClassInvoker.
This step fails here & It says no Account Found with PrimaryKey 0 & No SuchGroup Exists
3. Hence adding a new user in that company fails
stating : No default User Exists.


Code Which i Have Written
Company company=null;
if(Validator.isNotNull(tempClient.getCompanyId()) && tempClient.getCompanyId()>0){
company=companyLocalService.getCompany(tempClient.getCompanyId());
}else{
company=companyLocalService.createCompany(counterLocalService.increment(Company.class.getName()));
}
company.setWebId(tempClient.getWebId());
company.setVirtualHostname(tempClient.getVirtualHost());
company.setSystem(false);
company.setMaxUsers(tempClient.getMaxUsers());
company.setMx(tempClient.getMailDomain());
company.setActive(true);

if(Validator.isNotNull(tempClient.getCompanyId()) && tempClient.getCompanyId()>0){
company=companyLocalService.updateCompany(company);
}else{
company=companyLocalService.addCompany(company);
}

MethodKey methodKey = new MethodKey(ClassResolverUtil.resolveByPortalClassLoader("com.liferay.portal.util.PortalInstances"),"initCompany",new Class[] {ServletContext.class,String.class});
try {
PortalClassInvoker.invoke(true, methodKey,servletcontext,company.getWebId());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return company.getCompanyId();
thumbnail
Parth Ghiya, modificado hace 7 años.

RE: Not able to add User in a Instance After Creating a Company via Code. (Respuesta)

Junior Member Mensajes: 35 Fecha de incorporación: 2/07/13 Mensajes recientes
I Resolved the issue by making the following amendments.

• For Adding a Company I used following method CompanyLocalServiceUtil.addCompany with following parameters.
o Parameters:
 webId the the company's web domain
 virtualHostname the company's virtual host name
 mx the company's mail domain
 shardName the company's shard
 system whether the company is the very first company (i.e., the super company)
 maxUsers the max number of company users (optionally 0)
 active whether the company is active
o Reason:
This method internally manages Other processes such as
 Adding entry in account Entity
 Adding entry in virtual host Entity
• The PortalInstances.InitCompany should occur in a different transaction i.e. that shouldn’t be in the same LocalServiceImpl.
o Reason:
When we create a instance these application start up events occurs.
application.startup.events=com.liferay.portal.events.AddDefaultDataAction,com.liferay.portal.events.AppStartupAction,com.liferay.portal.events.ChannelHubAppStartupAction
Hence it should be in separate transaction.