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