1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.events;
21  
22  import com.liferay.portal.NoSuchUserException;
23  import com.liferay.portal.kernel.events.ActionException;
24  import com.liferay.portal.kernel.events.SimpleAction;
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.service.UserLocalServiceUtil;
31  
32  import java.util.Calendar;
33  import java.util.Locale;
34  
35  /**
36   * <a href="SampleAppStartupAction.java.html"><b><i>View Source</i></b></a>
37   *
38   * <p>
39   * This class can be used to populate an empty database programmatically. This
40   * allows a developer to create sample data without relying on native SQL.
41   * </p>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class SampleAppStartupAction extends SimpleAction {
47  
48      public void run(String[] ids) throws ActionException {
49          try {
50              long companyId = GetterUtil.getLong(ids[0]);
51  
52              doRun(companyId);
53          }
54          catch (Exception e) {
55              throw new ActionException(e);
56          }
57      }
58  
59      protected void doRun(long companyId) throws Exception {
60          try {
61              UserLocalServiceUtil.getUserByScreenName(companyId, "paul");
62  
63              // Do not populate the sample database if Paul already exists
64  
65              return;
66          }
67          catch (NoSuchUserException nsue) {
68          }
69  
70          long creatorUserId = 0;
71          boolean autoPassword = false;
72          String password1 = "test";
73          String password2 = password1;
74          boolean autoScreenName = false;
75          String screenName = "paul";
76          String emailAddress = "paul@liferay.com";
77          Locale locale = Locale.US;
78          String firstName = "Paul";
79          String middleName = StringPool.BLANK;
80          String lastName = "Smith";
81          int prefixId = 0;
82          int suffixId = 0;
83          boolean male = true;
84          int birthdayMonth = Calendar.JANUARY;
85          int birthdayDay = 1;
86          int birthdayYear = 1970;
87          String jobTitle = StringPool.BLANK;
88          long[] organizationIds = new long[0];
89          boolean sendEmail = false;
90  
91          User paulUser = UserLocalServiceUtil.addUser(
92              creatorUserId, companyId, autoPassword, password1, password2,
93              autoScreenName, screenName, emailAddress, locale, firstName,
94              middleName, lastName, prefixId, suffixId, male, birthdayMonth,
95              birthdayDay, birthdayYear, jobTitle, organizationIds, sendEmail);
96  
97          if (_log.isDebugEnabled()) {
98              _log.debug(
99                  paulUser.getFullName() + " was created with user id " +
100                     paulUser.getUserId());
101         }
102 
103         screenName = "jane";
104         emailAddress = "jane@liferay.com";
105         firstName = "Jane";
106 
107         User janeUser = UserLocalServiceUtil.addUser(
108             creatorUserId, companyId, autoPassword, password1, password2,
109             autoScreenName, screenName, emailAddress, locale, firstName,
110             middleName, lastName, prefixId, suffixId, male, birthdayMonth,
111             birthdayDay, birthdayYear, jobTitle, organizationIds, sendEmail);
112 
113         if (_log.isDebugEnabled()) {
114             _log.debug(
115                 janeUser.getFullName() + " was created with user id " +
116                     janeUser.getUserId());
117         }
118     }
119 
120     private static Log _log =
121          LogFactoryUtil.getLog(SampleAppStartupAction.class);
122 
123 }