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.kernel.test.util;
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    
087                    return OrganizationLocalServiceUtil.addOrganization(
088                            user.getUserId(), parentOrganizationId, name, site);
089            }
090    
091            public static OrgLabor addOrgLabor(Organization organization)
092                    throws Exception {
093    
094                    return OrgLaborLocalServiceUtil.addOrgLabor(
095                            organization.getOrganizationId(),
096                            _getListTypeId(ListTypeConstants.ORGANIZATION_SERVICE),
097                            RandomTestUtil.nextInt(), RandomTestUtil.nextInt(),
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            }
105    
106            public static PasswordPolicy addPasswordPolicyRel(
107                            Organization organization, ServiceContext serviceContext)
108                    throws Exception {
109    
110                    PasswordPolicy passwordPolicy =
111                            PasswordPolicyTestUtil.addPasswordPolicy(serviceContext);
112    
113                    PasswordPolicyRelLocalServiceUtil.addPasswordPolicyRel(
114                            passwordPolicy.getPasswordPolicyId(),
115                            organization.getModelClassName(), organization.getOrganizationId());
116    
117                    return passwordPolicy;
118            }
119    
120            public static Phone addPhone(Organization organization) throws Exception {
121                    return PhoneLocalServiceUtil.addPhone(
122                            organization.getUserId(), organization.getModelClassName(),
123                            organization.getOrganizationId(), "0000000000", "000",
124                            _getListTypeId(ListTypeConstants.ORGANIZATION_PHONE), false,
125                            new ServiceContext());
126            }
127    
128            public static Organization addSite(Organization organization)
129                    throws Exception {
130    
131                    return OrganizationLocalServiceUtil.updateOrganization(
132                            organization.getCompanyId(), organization.getOrganizationId(),
133                            organization.getParentOrganizationId(), organization.getName(),
134                            organization.getType(), organization.getRegionId(),
135                            organization.getCountryId(), organization.getStatusId(),
136                            organization.getComments(), false, null, true, null);
137            }
138    
139            public static Website addWebsite(Organization organization)
140                    throws Exception {
141    
142                    return WebsiteLocalServiceUtil.addWebsite(
143                            organization.getUserId(), organization.getModelClassName(),
144                            organization.getOrganizationId(), "http://www.test.com",
145                            _getListTypeId(ListTypeConstants.ORGANIZATION_WEBSITE), false,
146                            new ServiceContext());
147            }
148    
149            private static long _getListTypeId(String type) throws Exception {
150                    List<ListType> listTypes = ListTypeServiceUtil.getListTypes(type);
151    
152                    ListType listType = listTypes.get(0);
153    
154                    return listType.getListTypeId();
155            }
156    
157    }