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.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    
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                    if (UserLocalServiceUtil.fetchUserByScreenName(
055                                    companyId, "paul") != null) {
056    
057                            return;
058                    }
059    
060                    long creatorUserId = 0;
061                    boolean autoPassword = false;
062                    String password1 = "test";
063                    String password2 = password1;
064                    boolean autoScreenName = false;
065                    String screenName = "paul";
066                    String emailAddress = "paul@liferay.com";
067                    long facebookId = 0;
068                    String openId = StringPool.BLANK;
069                    Locale locale = LocaleUtil.US;
070                    String firstName = "Paul";
071                    String middleName = StringPool.BLANK;
072                    String lastName = "Smith";
073                    int prefixId = 0;
074                    int suffixId = 0;
075                    boolean male = true;
076                    int birthdayMonth = Calendar.JANUARY;
077                    int birthdayDay = 1;
078                    int birthdayYear = 1970;
079                    String jobTitle = StringPool.BLANK;
080                    long[] groupIds = null;
081                    long[] organizationIds = null;
082                    long[] roleIds = null;
083                    long[] userGroupIds = null;
084                    boolean sendEmail = false;
085    
086                    ServiceContext serviceContext = new ServiceContext();
087    
088                    User paulUser = UserLocalServiceUtil.addUser(
089                            creatorUserId, companyId, autoPassword, password1, password2,
090                            autoScreenName, screenName, emailAddress, facebookId, openId,
091                            locale, firstName, middleName, lastName, prefixId, suffixId, male,
092                            birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
093                            organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);
094    
095                    if (_log.isDebugEnabled()) {
096                            _log.debug(
097                                    paulUser.getFullName() + " was created with user id " +
098                                            paulUser.getUserId());
099                    }
100    
101                    screenName = "jane";
102                    emailAddress = "jane@liferay.com";
103                    firstName = "Jane";
104    
105                    User janeUser = UserLocalServiceUtil.addUser(
106                            creatorUserId, companyId, autoPassword, password1, password2,
107                            autoScreenName, screenName, emailAddress, facebookId, openId,
108                            locale, firstName, middleName, lastName, prefixId, suffixId, male,
109                            birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
110                            organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);
111    
112                    if (_log.isDebugEnabled()) {
113                            _log.debug(
114                                    janeUser.getFullName() + " was created with user id " +
115                                            janeUser.getUserId());
116                    }
117            }
118    
119            private static Log _log = LogFactoryUtil.getLog(
120                    SampleAppStartupAction.class);
121    
122    }