Fórum

User registration hook (AddUserWithWorkflow) creates Organization.

thumbnail
Marco Endres, modificado 7 Anos atrás.

User registration hook (AddUserWithWorkflow) creates Organization.

Regular Member Postagens: 112 Data de Entrada: 22/08/12 Postagens Recentes
Hello guys

I made a Servicewrapper that override the UserLocalServiceWrapper. I override the addUserWithWorkflow, because i want to create an organization when he register. But there is a problem. When i try to create an org after i created a User with super.addUserWithWorkflow i get a DuplicateOrganizationException. The org was already created with the right things. I also hooked the JSP and added some fields. These fields are also filled out. When i create the user with super.addUser() the org doesn't gets created once and and everything works fine.

When i only call the super.addUserWithWorkflow(..) an org gets created.

So my question:
What is the difference between those 2 Methodes?
Am I doing something wrong?
Why does a Org gets created?

package com.clavisit.faberplace.portal;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.osgi.service.component.annotations.Component;

import com.liferay.document.library.kernel.model.DLFolderConstants;
import com.liferay.dynamic.data.mapping.model.DDMStructure;
import com.liferay.dynamic.data.mapping.model.DDMTemplate;
import com.liferay.dynamic.data.mapping.service.DDMStructureLocalServiceUtil;
import com.liferay.expando.kernel.model.ExpandoBridge;
import com.liferay.expando.kernel.model.ExpandoColumnConstants;
import com.liferay.journal.model.JournalArticle;
import com.liferay.journal.service.JournalArticleLocalServiceUtil;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.model.ListType;
import com.liferay.portal.kernel.model.ListTypeConstants;
import com.liferay.portal.kernel.model.Organization;
import com.liferay.portal.kernel.model.OrganizationConstants;
import com.liferay.portal.kernel.model.RegionConstants;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.service.AddressLocalServiceUtil;
import com.liferay.portal.kernel.service.ClassNameLocalServiceUtil;
import com.liferay.portal.kernel.service.ListTypeServiceUtil;
import com.liferay.portal.kernel.service.OrganizationLocalServiceUtil;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.service.ServiceWrapper;
import com.liferay.portal.kernel.service.UserLocalServiceUtil;
import com.liferay.portal.kernel.service.UserLocalServiceWrapper;
import com.liferay.portal.kernel.util.OrderByComparatorFactoryUtil;

@Component(
	immediate = true,
	property = {
	},
	service = ServiceWrapper.class
)
public class UserLocalServiceOverride extends UserLocalServiceWrapper {
	
	private static final Log log = LogFactoryUtil.getLog(UserLocalServiceOverride.class);

	public UserLocalServiceOverride() {
		super(null);
	}
	
	@Override
	public User addUserWithWorkflow(
		long creatorUserId, long companyId, boolean autoPassword,
		String password1, String password2,
		boolean autoScreenName, String screenName,
		String emailAddress, long facebookId,
		String openId, java.util.Locale locale,
		String firstName, String middleName,
		String lastName, long prefixId, long suffixId, boolean male,
		int birthdayMonth, int birthdayDay, int birthdayYear,
		String jobTitle, long[] groupIds, long[] organizationIds,
		long[] roleIds, long[] userGroupIds, boolean sendEmail,
		ServiceContext serviceContext)
		throws PortalException {
		/**anfang adresse**/
		String street = "";
		String zipCode = "";
		String city = "";			
		long countryId;
		String company = "";
		long addressListTypeId = 0;
		User user = null;
		
		try {
			user =  super.addUserWithWorkflow(creatorUserId, companyId,
					autoPassword, password1, password2, autoScreenName, screenName,
					emailAddress, facebookId, openId, locale, firstName, middleName,
					lastName, prefixId, suffixId, male, birthdayMonth, birthdayDay,
					birthdayYear, jobTitle, groupIds, organizationIds, roleIds,
					userGroupIds, sendEmail, serviceContext);

		} catch (Exception e) {
			log.error("Can't create User", e);
		}

		street = (String) serviceContext.getAttribute("street");
		zipCode = (String) serviceContext.getAttribute("zipCode");
		city = (String) serviceContext.getAttribute("city");
		countryId = Long.parseLong((String) serviceContext.getAttribute("country"));
		company = (String) serviceContext.getAttribute("company");
			
			/** Create ORG **/
		
		try {
			Organization newOrg = OrganizationLocalServiceUtil.addOrganization(user.getUserId(), OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID, company, OrganizationConstants.TYPE_ORGANIZATION , RegionConstants.DEFAULT_REGION_ID, countryId, ListTypeConstants.ORGANIZATION_STATUS_DEFAULT, "", false, serviceContext);
			UserLocalServiceUtil.addOrganizationUser(newOrg.getOrganizationId(), user);
		} catch (Exception e) {
			log.error("Can't create Org with company name: " + company , e);
		}
      return user;
   }
}