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