Example usage for com.liferay.portal.kernel.service UserLocalService getUserByScreenName

List of usage examples for com.liferay.portal.kernel.service UserLocalService getUserByScreenName

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.service UserLocalService getUserByScreenName.

Prototype

@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public User getUserByScreenName(long companyId, String screenName) throws PortalException;

Source Link

Document

Returns the user with the screen name.

Usage

From source file:com.liferay.faces.demos.bean.TestSetupBackingBean.java

License:Open Source License

public void addUser(long creatorUserId, long groupId, long companyId, String firstName, String lastName)
        throws PortalException {

    boolean autoPassword = false;
    String password1 = "test";
    String password2 = password1;
    boolean autoScreenName = false;
    String screenName = firstName.toLowerCase() + "." + lastName.toLowerCase();
    String emailAddress = screenName + "@" + "liferay.com";
    long facebookId = 0L;
    String openId = "";
    Locale locale = Locale.ENGLISH;
    String middleName = "";
    int prefixId = 0;
    int suffixId = 0;
    boolean male = true;
    int birthdayMonth = 1;
    int birthdayDay = 1;
    int birthdayYear = 1970;
    String jobTitle = "";
    long[] groupIds = new long[] { groupId };
    long[] organizationIds = new long[] {};
    long[] roleIds = new long[] {};
    long[] userGroupIds = new long[] {};
    boolean sendEmail = false;
    ServiceContext serviceContext = new ServiceContext();
    UserLocalService userLocalService;

    if (userLocalServiceTracker.isEmpty()) {
        FacesContextHelperUtil.addGlobalErrorMessage("is-temporarily-unavailable", "User service");
    } else {//ww w.ja  va 2  s. co  m
        userLocalService = userLocalServiceTracker.getService();

        boolean addUser = false;

        try {
            User user = userLocalService.getUserByScreenName(companyId, screenName);

            if ("john.adams".equals(screenName)) {
                userLocalService.deleteUser(user);
                addUser = true;
            }
        } catch (NoSuchUserException e) {
            addUser = true;
        }

        if (addUser) {
            userLocalService.addUser(creatorUserId, companyId, autoPassword, password1, password2,
                    autoScreenName, screenName, emailAddress, facebookId, openId, locale, firstName, middleName,
                    lastName, prefixId, suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
                    groupIds, organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);
            logger.debug("Added user=[{0}]", screenName);
        }
    }
}