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