1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.events;
24  
25  import com.liferay.portal.NoSuchUserException;
26  import com.liferay.portal.kernel.events.ActionException;
27  import com.liferay.portal.kernel.events.SimpleAction;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.service.ServiceContext;
34  import com.liferay.portal.service.UserLocalServiceUtil;
35  
36  import java.util.Calendar;
37  import java.util.Locale;
38  
39  /**
40   * <a href="SampleAppStartupAction.java.html"><b><i>View Source</i></b></a>
41   *
42   * <p>
43   * This class can be used to populate an empty database programmatically. This
44   * allows a developer to create sample data without relying on native SQL.
45   * </p>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class SampleAppStartupAction extends SimpleAction {
51  
52      public void run(String[] ids) throws ActionException {
53          try {
54              long companyId = GetterUtil.getLong(ids[0]);
55  
56              doRun(companyId);
57          }
58          catch (Exception e) {
59              throw new ActionException(e);
60          }
61      }
62  
63      protected void doRun(long companyId) throws Exception {
64          try {
65              UserLocalServiceUtil.getUserByScreenName(companyId, "paul");
66  
67              // Do not populate the sample database if Paul already exists
68  
69              return;
70          }
71          catch (NoSuchUserException nsue) {
72          }
73  
74          long creatorUserId = 0;
75          boolean autoPassword = false;
76          String password1 = "test";
77          String password2 = password1;
78          boolean autoScreenName = false;
79          String screenName = "paul";
80          String emailAddress = "paul@liferay.com";
81          String openId = StringPool.BLANK;
82          Locale locale = Locale.US;
83          String firstName = "Paul";
84          String middleName = StringPool.BLANK;
85          String lastName = "Smith";
86          int prefixId = 0;
87          int suffixId = 0;
88          boolean male = true;
89          int birthdayMonth = Calendar.JANUARY;
90          int birthdayDay = 1;
91          int birthdayYear = 1970;
92          String jobTitle = StringPool.BLANK;
93          long[] groupIds = null;
94          long[] organizationIds = null;
95          long[] roleIds = null;
96          long[] userGroupIds = null;
97          boolean sendEmail = false;
98  
99          ServiceContext serviceContext = new ServiceContext();
100 
101         User paulUser = UserLocalServiceUtil.addUser(
102             creatorUserId, companyId, autoPassword, password1, password2,
103             autoScreenName, screenName, emailAddress, openId, locale, firstName,
104             middleName, lastName, prefixId, suffixId, male, birthdayMonth,
105             birthdayDay, birthdayYear, jobTitle, groupIds, organizationIds,
106             roleIds, userGroupIds, sendEmail, serviceContext);
107 
108         if (_log.isDebugEnabled()) {
109             _log.debug(
110                 paulUser.getFullName() + " was created with user id " +
111                     paulUser.getUserId());
112         }
113 
114         screenName = "jane";
115         emailAddress = "jane@liferay.com";
116         firstName = "Jane";
117 
118         User janeUser = UserLocalServiceUtil.addUser(
119             creatorUserId, companyId, autoPassword, password1, password2,
120             autoScreenName, screenName, emailAddress, openId, locale, firstName,
121             middleName, lastName, prefixId, suffixId, male, birthdayMonth,
122             birthdayDay, birthdayYear, jobTitle, groupIds, organizationIds,
123             roleIds, userGroupIds, sendEmail, serviceContext);
124 
125         if (_log.isDebugEnabled()) {
126             _log.debug(
127                 janeUser.getFullName() + " was created with user id " +
128                     janeUser.getUserId());
129         }
130     }
131 
132     private static Log _log =
133          LogFactoryUtil.getLog(SampleAppStartupAction.class);
134 
135 }