001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.events;
016    
017    import com.liferay.portal.kernel.events.ActionException;
018    import com.liferay.portal.kernel.events.SimpleAction;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.LocaleUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.service.UserLocalServiceUtil;
027    import com.liferay.util.PwdGenerator;
028    
029    import java.util.Calendar;
030    import java.util.Locale;
031    
032    /**
033     * <p>
034     * This class can be used to populate an empty database programmatically. This
035     * allows a developer to create sample data without relying on native SQL.
036     * </p>
037     *
038     * @author Brian Wing Shun Chan
039     */
040    public class SampleAppStartupAction extends SimpleAction {
041    
042            @Override
043            public void run(String[] ids) throws ActionException {
044                    try {
045                            long companyId = GetterUtil.getLong(ids[0]);
046    
047                            doRun(companyId);
048                    }
049                    catch (Exception e) {
050                            throw new ActionException(e);
051                    }
052            }
053    
054            protected void doRun(long companyId) throws Exception {
055                    if (UserLocalServiceUtil.fetchUserByScreenName(
056                                    companyId, "paul") != null) {
057    
058                            return;
059                    }
060    
061                    long creatorUserId = 0;
062                    boolean autoPassword = false;
063                    String password1 = PwdGenerator.getPassword();
064                    String password2 = password1;
065                    boolean autoScreenName = false;
066                    String screenName = "paul";
067                    String emailAddress = "paul@liferay.com";
068                    long facebookId = 0;
069                    String openId = StringPool.BLANK;
070                    Locale locale = LocaleUtil.US;
071                    String firstName = "Paul";
072                    String middleName = StringPool.BLANK;
073                    String lastName = "Smith";
074                    int prefixId = 0;
075                    int suffixId = 0;
076                    boolean male = true;
077                    int birthdayMonth = Calendar.JANUARY;
078                    int birthdayDay = 1;
079                    int birthdayYear = 1970;
080                    String jobTitle = StringPool.BLANK;
081                    long[] groupIds = null;
082                    long[] organizationIds = null;
083                    long[] roleIds = null;
084                    long[] userGroupIds = null;
085                    boolean sendEmail = false;
086    
087                    ServiceContext serviceContext = new ServiceContext();
088    
089                    User paulUser = UserLocalServiceUtil.addUser(
090                            creatorUserId, companyId, autoPassword, password1, password2,
091                            autoScreenName, screenName, emailAddress, facebookId, openId,
092                            locale, firstName, middleName, lastName, prefixId, suffixId, male,
093                            birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
094                            organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);
095    
096                    if (_log.isDebugEnabled()) {
097                            _log.debug(
098                                    paulUser.getFullName() + " was created with user id " +
099                                            paulUser.getUserId());
100                    }
101    
102                    screenName = "jane";
103                    emailAddress = "jane@liferay.com";
104                    firstName = "Jane";
105    
106                    User janeUser = UserLocalServiceUtil.addUser(
107                            creatorUserId, companyId, autoPassword, password1, password2,
108                            autoScreenName, screenName, emailAddress, facebookId, openId,
109                            locale, firstName, middleName, lastName, prefixId, suffixId, male,
110                            birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
111                            organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);
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 = LogFactoryUtil.getLog(
121                    SampleAppStartupAction.class);
122    
123    }