001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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            @Override
042            public void run(String[] ids) throws ActionException {
043                    try {
044                            long companyId = GetterUtil.getLong(ids[0]);
045    
046                            doRun(companyId);
047                    }
048                    catch (Exception e) {
049                            throw new ActionException(e);
050                    }
051            }
052    
053            protected void doRun(long companyId) throws Exception {
054                    try {
055                            UserLocalServiceUtil.getUserByScreenName(companyId, "paul");
056    
057                            // Do not populate the sample database if Paul already exists
058    
059                            return;
060                    }
061                    catch (NoSuchUserException nsue) {
062                    }
063    
064                    long creatorUserId = 0;
065                    boolean autoPassword = false;
066                    String password1 = "test";
067                    String password2 = password1;
068                    boolean autoScreenName = false;
069                    String screenName = "paul";
070                    String emailAddress = "paul@liferay.com";
071                    long facebookId = 0;
072                    String openId = StringPool.BLANK;
073                    Locale locale = Locale.US;
074                    String firstName = "Paul";
075                    String middleName = StringPool.BLANK;
076                    String lastName = "Smith";
077                    int prefixId = 0;
078                    int suffixId = 0;
079                    boolean male = true;
080                    int birthdayMonth = Calendar.JANUARY;
081                    int birthdayDay = 1;
082                    int birthdayYear = 1970;
083                    String jobTitle = StringPool.BLANK;
084                    long[] groupIds = null;
085                    long[] organizationIds = null;
086                    long[] roleIds = null;
087                    long[] userGroupIds = null;
088                    boolean sendEmail = false;
089    
090                    ServiceContext serviceContext = new ServiceContext();
091    
092                    User paulUser = UserLocalServiceUtil.addUser(
093                            creatorUserId, companyId, autoPassword, password1, password2,
094                            autoScreenName, screenName, emailAddress, facebookId, openId,
095                            locale, firstName, middleName, lastName, prefixId, suffixId, male,
096                            birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
097                            organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);
098    
099                    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, facebookId, openId,
112                            locale, firstName, middleName, lastName, prefixId, suffixId, male,
113                            birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
114                            organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);
115    
116                    if (_log.isDebugEnabled()) {
117                            _log.debug(
118                                    janeUser.getFullName() + " was created with user id " +
119                                            janeUser.getUserId());
120                    }
121            }
122    
123            private static Log _log = LogFactoryUtil.getLog(
124                    SampleAppStartupAction.class);
125    
126    }