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