1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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 }