1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.events;
16  
17  import com.liferay.portal.NoSuchUserException;
18  import com.liferay.portal.kernel.events.ActionException;
19  import com.liferay.portal.kernel.events.SimpleAction;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.kernel.util.GetterUtil;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.model.User;
25  import com.liferay.portal.service.ServiceContext;
26  import com.liferay.portal.service.UserLocalServiceUtil;
27  
28  import java.util.Calendar;
29  import java.util.Locale;
30  
31  /**
32   * <a href="SampleAppStartupAction.java.html"><b><i>View Source</i></b></a>
33   *
34   * <p>
35   * This class can be used to populate an empty database programmatically. This
36   * allows a developer to create sample data without relying on native SQL.
37   * </p>
38   *
39   * @author Brian Wing Shun Chan
40   */
41  public class SampleAppStartupAction extends SimpleAction {
42  
43      public void run(String[] ids) throws ActionException {
44          try {
45              long companyId = GetterUtil.getLong(ids[0]);
46  
47              doRun(companyId);
48          }
49          catch (Exception e) {
50              throw new ActionException(e);
51          }
52      }
53  
54      protected void doRun(long companyId) throws Exception {
55          try {
56              UserLocalServiceUtil.getUserByScreenName(companyId, "paul");
57  
58              // Do not populate the sample database if Paul already exists
59  
60              return;
61          }
62          catch (NoSuchUserException nsue) {
63          }
64  
65          long creatorUserId = 0;
66          boolean autoPassword = false;
67          String password1 = "test";
68          String password2 = password1;
69          boolean autoScreenName = false;
70          String screenName = "paul";
71          String emailAddress = "paul@liferay.com";
72          String openId = StringPool.BLANK;
73          Locale locale = Locale.US;
74          String firstName = "Paul";
75          String middleName = StringPool.BLANK;
76          String lastName = "Smith";
77          int prefixId = 0;
78          int suffixId = 0;
79          boolean male = true;
80          int birthdayMonth = Calendar.JANUARY;
81          int birthdayDay = 1;
82          int birthdayYear = 1970;
83          String jobTitle = StringPool.BLANK;
84          long[] groupIds = null;
85          long[] organizationIds = null;
86          long[] roleIds = null;
87          long[] userGroupIds = null;
88          boolean sendEmail = false;
89  
90          ServiceContext serviceContext = new ServiceContext();
91  
92          User paulUser = UserLocalServiceUtil.addUser(
93              creatorUserId, companyId, autoPassword, password1, password2,
94              autoScreenName, screenName, emailAddress, openId, locale, firstName,
95              middleName, lastName, prefixId, suffixId, male, birthdayMonth,
96              birthdayDay, birthdayYear, jobTitle, groupIds, organizationIds,
97              roleIds, userGroupIds, sendEmail, serviceContext);
98  
99          if (_log.isDebugEnabled()) {
100             _log.debug(
101                 paulUser.getFullName() + " was created with user id " +
102                     paulUser.getUserId());
103         }
104 
105         screenName = "jane";
106         emailAddress = "jane@liferay.com";
107         firstName = "Jane";
108 
109         User janeUser = UserLocalServiceUtil.addUser(
110             creatorUserId, companyId, autoPassword, password1, password2,
111             autoScreenName, screenName, emailAddress, openId, locale, firstName,
112             middleName, lastName, prefixId, suffixId, male, birthdayMonth,
113             birthdayDay, birthdayYear, jobTitle, groupIds, organizationIds,
114             roleIds, userGroupIds, sendEmail, serviceContext);
115 
116         if (_log.isDebugEnabled()) {
117             _log.debug(
118                 janeUser.getFullName() + " was created with user id " +
119                     janeUser.getUserId());
120         }
121     }
122 
123     private static Log _log = LogFactoryUtil.getLog(
124         SampleAppStartupAction.class);
125 
126 }