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.UserLocalServiceUtil;
26  
27  import java.util.Calendar;
28  import java.util.Locale;
29  
30  /**
31   * <a href="SampleAppStartupAction.java.html"><b><i>View Source</i></b></a>
32   *
33   * <p>
34   * This class can be used to populate an empty database programmatically. This
35   * allows a developer to create sample data without relying on native SQL.
36   * </p>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class SampleAppStartupAction extends SimpleAction {
41  
42      public void run(String[] ids) throws ActionException {
43          try {
44              long companyId = GetterUtil.getLong(ids[0]);
45  
46              doRun(companyId);
47          }
48          catch (Exception e) {
49              throw new ActionException(e);
50          }
51      }
52  
53      protected void doRun(long companyId) throws Exception {
54          try {
55              UserLocalServiceUtil.getUserByScreenName(companyId, "paul");
56  
57              // Do not populate the sample database if Paul already exists
58  
59              return;
60          }
61          catch (NoSuchUserException nsue) {
62          }
63  
64          long creatorUserId = 0;
65          boolean autoPassword = false;
66          String password1 = "test";
67          String password2 = password1;
68          boolean autoScreenName = false;
69          String screenName = "paul";
70          String emailAddress = "paul@liferay.com";
71          Locale locale = Locale.US;
72          String firstName = "Paul";
73          String middleName = StringPool.BLANK;
74          String lastName = "Smith";
75          int prefixId = 0;
76          int suffixId = 0;
77          boolean male = true;
78          int birthdayMonth = Calendar.JANUARY;
79          int birthdayDay = 1;
80          int birthdayYear = 1970;
81          String jobTitle = StringPool.BLANK;
82          long[] organizationIds = new long[0];
83          boolean sendEmail = false;
84  
85          User paulUser = UserLocalServiceUtil.addUser(
86              creatorUserId, companyId, autoPassword, password1, password2,
87              autoScreenName, screenName, emailAddress, locale, firstName,
88              middleName, lastName, prefixId, suffixId, male, birthdayMonth,
89              birthdayDay, birthdayYear, jobTitle, organizationIds, sendEmail);
90  
91          if (_log.isDebugEnabled()) {
92              _log.debug(
93                  paulUser.getFullName() + " was created with user id " +
94                      paulUser.getUserId());
95          }
96  
97          screenName = "jane";
98          emailAddress = "jane@liferay.com";
99          firstName = "Jane";
100 
101         User janeUser = UserLocalServiceUtil.addUser(
102             creatorUserId, companyId, autoPassword, password1, password2,
103             autoScreenName, screenName, emailAddress, locale, firstName,
104             middleName, lastName, prefixId, suffixId, male, birthdayMonth,
105             birthdayDay, birthdayYear, jobTitle, organizationIds, sendEmail);
106 
107         if (_log.isDebugEnabled()) {
108             _log.debug(
109                 janeUser.getFullName() + " was created with user id " +
110                     janeUser.getUserId());
111         }
112     }
113 
114     private static Log _log = LogFactoryUtil.getLog(
115         SampleAppStartupAction.class);
116 
117 }