001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.util.test;
016    
017    import com.liferay.portal.model.Address;
018    import com.liferay.portal.model.EmailAddress;
019    import com.liferay.portal.model.ListType;
020    import com.liferay.portal.model.ListTypeConstants;
021    import com.liferay.portal.model.OrgLabor;
022    import com.liferay.portal.model.Organization;
023    import com.liferay.portal.model.OrganizationConstants;
024    import com.liferay.portal.model.PasswordPolicy;
025    import com.liferay.portal.model.Phone;
026    import com.liferay.portal.model.User;
027    import com.liferay.portal.model.Website;
028    import com.liferay.portal.service.AddressLocalServiceUtil;
029    import com.liferay.portal.service.EmailAddressLocalServiceUtil;
030    import com.liferay.portal.service.ListTypeServiceUtil;
031    import com.liferay.portal.service.OrgLaborLocalServiceUtil;
032    import com.liferay.portal.service.OrganizationLocalServiceUtil;
033    import com.liferay.portal.service.PasswordPolicyRelLocalServiceUtil;
034    import com.liferay.portal.service.PhoneLocalServiceUtil;
035    import com.liferay.portal.service.ServiceContext;
036    import com.liferay.portal.service.WebsiteLocalServiceUtil;
037    import com.liferay.portlet.passwordpoliciesadmin.util.test.PasswordPolicyTestUtil;
038    
039    import java.util.List;
040    
041    /**
042     * @author Alberto Chaparro
043     */
044    public class OrganizationTestUtil {
045    
046            public static Address addAddress(Organization organization)
047                    throws Exception {
048    
049                    return AddressLocalServiceUtil.addAddress(
050                            organization.getUserId(), organization.getModelClassName(),
051                            organization.getOrganizationId(), RandomTestUtil.randomString(),
052                            RandomTestUtil.randomString(), RandomTestUtil.randomString(),
053                            RandomTestUtil.randomString(), RandomTestUtil.randomString(),
054                            RandomTestUtil.nextLong(), RandomTestUtil.randomLong(),
055                            _getListTypeId(ListTypeConstants.ORGANIZATION_ADDRESS), false,
056                            false, new ServiceContext());
057            }
058    
059            public static EmailAddress addEmailAddress(Organization organization)
060                    throws Exception {
061    
062                    return EmailAddressLocalServiceUtil.addEmailAddress(
063                            organization.getUserId(), organization.getModelClassName(),
064                            organization.getOrganizationId(), "test@liferay.com",
065                            _getListTypeId(ListTypeConstants.ORGANIZATION_EMAIL_ADDRESS), false,
066                            new ServiceContext());
067            }
068    
069            public static Organization addOrganization() throws Exception {
070                    return addOrganization(
071                            OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID,
072                            RandomTestUtil.randomString(), false);
073            }
074    
075            public static Organization addOrganization(boolean site) throws Exception {
076                    return addOrganization(
077                            OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID,
078                            RandomTestUtil.randomString(), site);
079            }
080    
081            public static Organization addOrganization(
082                            long parentOrganizationId, String name, boolean site)
083                    throws Exception {
084    
085                    User user = UserTestUtil.addUser(
086                            RandomTestUtil.randomString(), false, null);
087    
088                    return OrganizationLocalServiceUtil.addOrganization(
089                            user.getUserId(), parentOrganizationId, name, site);
090            }
091    
092            public static OrgLabor addOrgLabor(Organization organization)
093                    throws Exception {
094    
095                    return OrgLaborLocalServiceUtil.addOrgLabor(
096                            organization.getOrganizationId(),
097                            _getListTypeId(ListTypeConstants.ORGANIZATION_SERVICE),
098                            RandomTestUtil.nextInt(), RandomTestUtil.nextInt(),
099                            RandomTestUtil.nextInt(), RandomTestUtil.nextInt(),
100                            RandomTestUtil.nextInt(), RandomTestUtil.nextInt(),
101                            RandomTestUtil.nextInt(), RandomTestUtil.nextInt(),
102                            RandomTestUtil.nextInt(), RandomTestUtil.nextInt(),
103                            RandomTestUtil.nextInt(), RandomTestUtil.nextInt(),
104                            RandomTestUtil.nextInt(), RandomTestUtil.nextInt());
105            }
106    
107            public static PasswordPolicy addPasswordPolicyRel(
108                            Organization organization, ServiceContext serviceContext)
109                    throws Exception {
110    
111                    PasswordPolicy passwordPolicy =
112                            PasswordPolicyTestUtil.addPasswordPolicy(serviceContext);
113    
114                    PasswordPolicyRelLocalServiceUtil.addPasswordPolicyRel(
115                            passwordPolicy.getPasswordPolicyId(),
116                            organization.getModelClassName(), organization.getOrganizationId());
117    
118                    return passwordPolicy;
119            }
120    
121            public static Phone addPhone(Organization organization) throws Exception {
122                    return PhoneLocalServiceUtil.addPhone(
123                            organization.getUserId(), organization.getModelClassName(),
124                            organization.getOrganizationId(), "0000000000", "000",
125                            _getListTypeId(ListTypeConstants.ORGANIZATION_PHONE), false,
126                            new ServiceContext());
127            }
128    
129            public static Organization addSite(Organization organization)
130                    throws Exception {
131    
132                    return OrganizationLocalServiceUtil.updateOrganization(
133                            organization.getCompanyId(), organization.getOrganizationId(),
134                            organization.getParentOrganizationId(), organization.getName(),
135                            organization.getType(), organization.getRegionId(),
136                            organization.getCountryId(), organization.getStatusId(),
137                            organization.getComments(), false, null, true, null);
138            }
139    
140            public static Website addWebsite(Organization organization)
141                    throws Exception {
142    
143                    return WebsiteLocalServiceUtil.addWebsite(
144                            organization.getUserId(), organization.getModelClassName(),
145                            organization.getOrganizationId(), "http://www.test.com",
146                            _getListTypeId(ListTypeConstants.ORGANIZATION_WEBSITE), false,
147                            new ServiceContext());
148            }
149    
150            private static int _getListTypeId(String type) throws Exception {
151                    List<ListType> listTypes = ListTypeServiceUtil.getListTypes(type);
152    
153                    ListType listType = listTypes.get(0);
154    
155                    return listType.getListTypeId();
156            }
157    
158    }