001    /**
002     * Copyright (c) 2000-2010 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.NoSuchUserException;
018    import com.liferay.portal.kernel.events.ActionException;
019    import com.liferay.portal.kernel.events.SimpleAction;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.GetterUtil;
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    
028    import java.util.Calendar;
029    import java.util.Locale;
030    
031    /**
032     * <p>
033     * This class can be used to populate an empty database programmatically. This
034     * allows a developer to create sample data without relying on native SQL.
035     * </p>
036     *
037     * @author Brian Wing Shun Chan
038     */
039    public class SampleAppStartupAction extends SimpleAction {
040    
041            public void run(String[] ids) throws ActionException {
042                    try {
043                            long companyId = GetterUtil.getLong(ids[0]);
044    
045                            doRun(companyId);
046                    }
047                    catch (Exception e) {
048                            throw new ActionException(e);
049                    }
050            }
051    
052            protected void doRun(long companyId) throws Exception {
053                    try {
054                            UserLocalServiceUtil.getUserByScreenName(companyId, "paul");
055    
056                            // Do not populate the sample database if Paul already exists
057    
058                            return;
059                    }
060                    catch (NoSuchUserException nsue) {
061                    }
062    
063                    long creatorUserId = 0;
064                    boolean autoPassword = false;
065                    String password1 = "test";
066                    String password2 = password1;
067                    boolean autoScreenName = false;
068                    String screenName = "paul";
069                    String emailAddress = "paul@liferay.com";
070                    long facebookId = 0;
071                    String openId = StringPool.BLANK;
072                    Locale locale = Locale.US;
073                    String firstName = "Paul";
074                    String middleName = StringPool.BLANK;
075                    String lastName = "Smith";
076                    int prefixId = 0;
077                    int suffixId = 0;
078                    boolean male = true;
079                    int birthdayMonth = Calendar.JANUARY;
080                    int birthdayDay = 1;
081                    int birthdayYear = 1970;
082                    String jobTitle = StringPool.BLANK;
083                    long[] groupIds = null;
084                    long[] organizationIds = null;
085                    long[] roleIds = null;
086                    long[] userGroupIds = null;
087                    boolean sendEmail = false;
088    
089                    ServiceContext serviceContext = new ServiceContext();
090    
091                    User paulUser = UserLocalServiceUtil.addUser(
092                            creatorUserId, companyId, autoPassword, password1, password2,
093                            autoScreenName, screenName, emailAddress, facebookId, openId,
094                            locale, firstName, middleName, lastName, prefixId, suffixId, male,
095                            birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
096                            organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);
097    
098                    if (_log.isDebugEnabled()) {
099                            _log.debug(
100                                    paulUser.getFullName() + " was created with user id " +
101                                            paulUser.getUserId());
102                    }
103    
104                    screenName = "jane";
105                    emailAddress = "jane@liferay.com";
106                    firstName = "Jane";
107    
108                    User janeUser = UserLocalServiceUtil.addUser(
109                            creatorUserId, companyId, autoPassword, password1, password2,
110                            autoScreenName, screenName, emailAddress, facebookId, openId,
111                            locale, firstName, middleName, lastName, prefixId, suffixId, male,
112                            birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
113                            organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);
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 = LogFactoryUtil.getLog(
123                    SampleAppStartupAction.class);
124    
125    }