1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class SampleAppStartupAction extends SimpleAction {
49  
50      public void run(String[] ids) throws ActionException {
51          try {
52              long companyId = GetterUtil.getLong(ids[0]);
53  
54              doRun(companyId);
55          }
56          catch (Exception e) {
57              throw new ActionException(e);
58          }
59      }
60  
61      protected void doRun(long companyId) throws Exception {
62          try {
63              UserLocalServiceUtil.getUserByScreenName(companyId, "paul");
64  
65              // Do not populate the sample database if Paul already exists
66  
67              return;
68          }
69          catch (NoSuchUserException nsue) {
70          }
71  
72          long creatorUserId = 0;
73          boolean autoPassword = false;
74          String password1 = "test";
75          String password2 = password1;
76          boolean autoScreenName = false;
77          String screenName = "paul";
78          String emailAddress = "paul@liferay.com";
79          Locale locale = Locale.US;
80          String firstName = "Paul";
81          String middleName = StringPool.BLANK;
82          String lastName = "Smith";
83          int prefixId = 0;
84          int suffixId = 0;
85          boolean male = true;
86          int birthdayMonth = Calendar.JANUARY;
87          int birthdayDay = 1;
88          int birthdayYear = 1970;
89          String jobTitle = StringPool.BLANK;
90          long[] organizationIds = new long[0];
91          boolean sendEmail = false;
92  
93          User paulUser = UserLocalServiceUtil.addUser(
94              creatorUserId, companyId, autoPassword, password1, password2,
95              autoScreenName, screenName, emailAddress, locale, firstName,
96              middleName, lastName, prefixId, suffixId, male, birthdayMonth,
97              birthdayDay, birthdayYear, jobTitle, organizationIds, sendEmail);
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, locale, firstName,
112             middleName, lastName, prefixId, suffixId, male, birthdayMonth,
113             birthdayDay, birthdayYear, jobTitle, organizationIds, sendEmail);
114 
115         if (_log.isDebugEnabled()) {
116             _log.debug(
117                 janeUser.getFullName() + " was created with user id " +
118                     janeUser.getUserId());
119         }
120     }
121 
122     private static Log _log =
123         LogFactoryUtil.getLog(SampleAppStartupAction.class);
124 
125 }