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.setup;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.FriendlyURLNormalizerUtil;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.model.Account;
024    import com.liferay.portal.model.Company;
025    import com.liferay.portal.model.Layout;
026    import com.liferay.portal.model.LayoutConstants;
027    import com.liferay.portal.model.ListTypeConstants;
028    import com.liferay.portal.model.Organization;
029    import com.liferay.portal.model.OrganizationConstants;
030    import com.liferay.portal.model.User;
031    import com.liferay.portal.service.AccountLocalServiceUtil;
032    import com.liferay.portal.service.CompanyLocalServiceUtil;
033    import com.liferay.portal.service.GroupLocalServiceUtil;
034    import com.liferay.portal.service.LayoutLocalServiceUtil;
035    import com.liferay.portal.service.OrganizationLocalServiceUtil;
036    import com.liferay.portal.service.ServiceContext;
037    import com.liferay.portal.service.UserLocalServiceUtil;
038    
039    import java.util.Calendar;
040    
041    import org.apache.commons.lang.time.StopWatch;
042    
043    /**
044     * @author Shinn Lok
045     */
046    public class SetupWizardSampleDataUtil {
047    
048            public static void addSampleData(long companyId) throws Exception {
049                    StopWatch stopWatch = new StopWatch();
050    
051                    stopWatch.start();
052    
053                    if (_log.isInfoEnabled()) {
054                            _log.info("Adding sample data");
055                    }
056    
057                    Company company = CompanyLocalServiceUtil.getCompanyById(companyId);
058    
059                    Account account = company.getAccount();
060    
061                    account.setName("Liferay");
062                    account.setLegalName("Liferay, Inc");
063    
064                    AccountLocalServiceUtil.updateAccount(account);
065    
066                    User defaultUser = company.getDefaultUser();
067    
068                    Organization organization =
069                            OrganizationLocalServiceUtil.addOrganization(
070                                    defaultUser.getUserId(),
071                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID,
072                                    "Liferay, Inc.", true);
073    
074                    GroupLocalServiceUtil.updateFriendlyURL(
075                            organization.getGroupId(), "/liferay");
076    
077                    Layout extranetLayout = LayoutLocalServiceUtil.addLayout(
078                            defaultUser.getUserId(), organization.getGroupId(), false,
079                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, "Liferay, Inc. Extranet",
080                            null, null, LayoutConstants.TYPE_PORTLET, false, "/extranet",
081                            new ServiceContext());
082    
083                    LayoutLocalServiceUtil.updateLayout(
084                            extranetLayout.getGroupId(), false, extranetLayout.getLayoutId(),
085                            extranetLayout.getTypeSettings());
086    
087                    Layout intranetLayout = LayoutLocalServiceUtil.addLayout(
088                            defaultUser.getUserId(), organization.getGroupId(), true,
089                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, "Liferay, Inc. Intranet",
090                            null, null, LayoutConstants.TYPE_PORTLET, false, "/intranet",
091                            new ServiceContext());
092    
093                    LayoutLocalServiceUtil.updateLayout(
094                            intranetLayout.getGroupId(), true, intranetLayout.getLayoutId(),
095                            intranetLayout.getTypeSettings());
096    
097                    User user = UserLocalServiceUtil.fetchUserByEmailAddress(
098                            company.getCompanyId(), "test@liferay.com");
099    
100                    if (user == null) {
101                            user = UserLocalServiceUtil.addDefaultAdminUser(
102                                    companyId, "joebloggs", "test@liferay.com",
103                                    LocaleUtil.getDefault(), "Joe", StringPool.BLANK, "Bloggs");
104                    }
105                    else {
106                            user.setScreenName("joebloggs");
107                            user.setGreeting("Welcome Joe Bloggs!");
108                            user.setFirstName("Joe");
109                            user.setLastName("Bloggs");
110                    }
111    
112                    user.setPasswordReset(false);
113    
114                    UserLocalServiceUtil.updateUser(user);
115    
116                    OrganizationLocalServiceUtil.addUserOrganization(
117                            user.getUserId(), organization);
118    
119                    addOrganizations(defaultUser, organization);
120    
121                    if (_log.isInfoEnabled()) {
122                            _log.info("Finished adding data in " + stopWatch.getTime() + " ms");
123                    }
124            }
125    
126            protected static void addOrganizations(
127                            User defaultUser, Organization parentOrganization)
128                    throws Exception {
129    
130                    for (Object[] organizationArray : _ORGANIZATION_ARRAYS) {
131                            String name = "Liferay " + organizationArray[0];
132                            long regionId = (Long)organizationArray[1];
133                            long countryId = (Long)organizationArray[2];
134                            String type = (String)organizationArray[3];
135    
136                            Organization organization =
137                                    OrganizationLocalServiceUtil.addOrganization(
138                                            defaultUser.getUserId(),
139                                            parentOrganization.getOrganizationId(), name, type,
140                                            regionId, countryId,
141                                            ListTypeConstants.ORGANIZATION_STATUS_DEFAULT,
142                                            StringPool.BLANK, true, null);
143    
144                            GroupLocalServiceUtil.updateFriendlyURL(
145                                    organization.getGroupId(),
146                                    FriendlyURLNormalizerUtil.normalize(
147                                            StringPool.SLASH + organizationArray[0]));
148    
149                            if (organizationArray.length <= 4) {
150                                    continue;
151                            }
152    
153                            String organizationPrefix = (String)organizationArray[4];
154    
155                            long[] groupIds = {organization.getGroupId()};
156                            long[] organizationIds = {
157                                    parentOrganization.getOrganizationId(),
158                                    organization.getOrganizationId()
159                            };
160    
161                            for (int i = 1; i <= 10; i++) {
162                                    String screenName = organizationPrefix + i;
163    
164                                    StringBundler sb = new StringBundler(5);
165    
166                                    sb.append("test.");
167                                    sb.append(organizationPrefix);
168                                    sb.append(StringPool.PERIOD);
169                                    sb.append(i);
170                                    sb.append("@liferay.com");
171    
172                                    String emailAddress = sb.toString();
173    
174                                    String lastName = organizationPrefix + StringPool.SPACE + i;
175    
176                                    User user = UserLocalServiceUtil.addUser(
177                                            0, defaultUser.getCompanyId(), false, "test", "test", false,
178                                            screenName, emailAddress, 0, null, LocaleUtil.getDefault(),
179                                            "Test", null, lastName, 0, 0, true, Calendar.JANUARY, 1,
180                                            1970, null, groupIds, organizationIds, null, null, false,
181                                            new ServiceContext());
182    
183                                    user.setPasswordReset(false);
184                                    user.setAgreedToTermsOfUse(true);
185    
186                                    UserLocalServiceUtil.updateUser(user);
187                            }
188                    }
189            }
190    
191            private static final Object[][] _ORGANIZATION_ARRAYS = {
192                    {
193                            "Chicago", 19014L, 19L, OrganizationConstants.TYPE_LOCATION, "ORD"
194                    },
195                    {
196                            "Consulting", 19005L, 19L,
197                            OrganizationConstants.TYPE_REGULAR_ORGANIZATION
198                    },
199                    {
200                            "Dalian", 0L, 2L, OrganizationConstants.TYPE_LOCATION, "DLC"
201                    },
202                    {
203                            "Engineering", 19005L, 19L,
204                            OrganizationConstants.TYPE_REGULAR_ORGANIZATION
205                    },
206                    {
207                            "Frankfurt", 0L, 4L, OrganizationConstants.TYPE_LOCATION, "FRA"
208                    },
209                    {
210                            "Hong Kong", 0L, 2L, OrganizationConstants.TYPE_LOCATION, "HKG"
211                    },
212                    {
213                            "Kuala Lumpur", 0L, 135L, OrganizationConstants.TYPE_LOCATION, "KUL"
214                    },
215                    {
216                            "Los Angeles", 19005L, 19L, OrganizationConstants.TYPE_LOCATION,
217                            "LAX"
218                    },
219                    {
220                            "Madrid", 0L, 15L, OrganizationConstants.TYPE_LOCATION, "MAD"
221                    },
222                    {
223                            "Marketing", 19005L, 19L,
224                            OrganizationConstants.TYPE_REGULAR_ORGANIZATION
225                    },
226                    {
227                            "New York", 19033L, 19L, OrganizationConstants.TYPE_LOCATION, "NYC"
228                    },
229                    {
230                            "Saint Paulo", 0L, 48L, OrganizationConstants.TYPE_LOCATION, "GRU"
231                    },
232                    {
233                            "Sales", 19005L, 19L,
234                            OrganizationConstants.TYPE_REGULAR_ORGANIZATION
235                    },
236                    {
237                            "San Francisco", 19005L, 19L, OrganizationConstants.TYPE_LOCATION,
238                            "SFO"
239                    },
240                    {
241                            "Support", 19005L, 19L,
242                            OrganizationConstants.TYPE_REGULAR_ORGANIZATION
243                    }
244            };
245    
246            private static final Log _log = LogFactoryUtil.getLog(
247                    SetupWizardSampleDataUtil.class);
248    
249    }