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