Search in sources :

Example 36 with ServiceContext

use of com.liferay.portal.service.ServiceContext in project liferay-ide by liferay.

the class UserLocalServiceImpl method updateIncompleteUser.

/**
 * Updates a user account that was automatically created when a guest user
 * participated in an action (e.g. posting a comment) and only provided his
 * name and email address.
 *
 * @param  creatorUserId the primary key of the creator
 * @param  companyId the primary key of the user's company
 * @param  autoPassword whether a password should be automatically generated
 *         for the user
 * @param  password1 the user's password
 * @param  password2 the user's password confirmation
 * @param  autoScreenName whether a screen name should be automatically
 *         generated for the user
 * @param  screenName the user's screen name
 * @param  emailAddress the user's email address
 * @param  facebookId the user's facebook ID
 * @param  openId the user's OpenID
 * @param  locale the user's locale
 * @param  firstName the user's first name
 * @param  middleName the user's middle name
 * @param  lastName the user's last name
 * @param  prefixId the user's name prefix ID
 * @param  suffixId the user's name suffix ID
 * @param  male whether the user is male
 * @param  birthdayMonth the user's birthday month (0-based, meaning 0 for
 *         January)
 * @param  birthdayDay the user's birthday day
 * @param  birthdayYear the user's birthday year
 * @param  jobTitle the user's job title
 * @param  updateUserInformation whether to update the user's information
 * @param  sendEmail whether to send the user an email notification about
 *         their new account
 * @param  serviceContext the service context to be applied (optionally
 *         <code>null</code>). Can set expando bridge attributes for the
 *         user.
 * @return the user
 * @throws PortalException if the user's information was invalid
 * @throws SystemException if a system exception occurred
 */
@Override
public User updateIncompleteUser(long creatorUserId, long companyId, boolean autoPassword, String password1, String password2, boolean autoScreenName, String screenName, String emailAddress, long facebookId, String openId, Locale locale, String firstName, String middleName, String lastName, int prefixId, int suffixId, boolean male, int birthdayMonth, int birthdayDay, int birthdayYear, String jobTitle, boolean updateUserInformation, boolean sendEmail, ServiceContext serviceContext) throws PortalException, SystemException {
    User user = getUserByEmailAddress(companyId, emailAddress);
    if (user.getStatus() != WorkflowConstants.STATUS_INCOMPLETE) {
        throw new PortalException("Invalid user status");
    }
    User defaultUser = getDefaultUser(companyId);
    if (facebookId > 0) {
        autoPassword = false;
        if ((password1 == null) || (password2 == null)) {
            password1 = PwdGenerator.getPassword();
            password2 = password1;
        }
        sendEmail = false;
    }
    if (updateUserInformation) {
        autoScreenName = false;
        if (PrefsPropsUtil.getBoolean(companyId, PropsKeys.USERS_SCREEN_NAME_ALWAYS_AUTOGENERATE)) {
            autoScreenName = true;
        }
        validate(companyId, user.getUserId(), autoPassword, password1, password2, autoScreenName, screenName, emailAddress, openId, firstName, middleName, lastName, null);
        if (!autoPassword) {
            if (Validator.isNull(password1) || Validator.isNull(password2)) {
                throw new UserPasswordException(UserPasswordException.PASSWORD_INVALID);
            }
        }
        if (autoScreenName) {
            ScreenNameGenerator screenNameGenerator = ScreenNameGeneratorFactory.getInstance();
            try {
                screenName = screenNameGenerator.generate(companyId, user.getUserId(), emailAddress);
            } catch (Exception e) {
                throw new SystemException(e);
            }
        }
        FullNameGenerator fullNameGenerator = FullNameGeneratorFactory.getInstance();
        String fullName = fullNameGenerator.getFullName(firstName, middleName, lastName);
        String greeting = LanguageUtil.format(locale, "welcome-x", " " + fullName, false);
        if (Validator.isNotNull(password1)) {
            user.setPassword(PasswordEncryptorUtil.encrypt(password1));
            user.setPasswordUnencrypted(password1);
        }
        user.setPasswordEncrypted(true);
        PasswordPolicy passwordPolicy = defaultUser.getPasswordPolicy();
        if ((passwordPolicy != null) && passwordPolicy.isChangeable() && passwordPolicy.isChangeRequired()) {
            user.setPasswordReset(true);
        } else {
            user.setPasswordReset(false);
        }
        user.setScreenName(screenName);
        user.setFacebookId(facebookId);
        user.setOpenId(openId);
        user.setLanguageId(locale.toString());
        user.setTimeZoneId(defaultUser.getTimeZoneId());
        user.setGreeting(greeting);
        user.setFirstName(firstName);
        user.setMiddleName(middleName);
        user.setLastName(lastName);
        user.setJobTitle(jobTitle);
        user.setExpandoBridgeAttributes(serviceContext);
        Date birthday = getBirthday(birthdayMonth, birthdayDay, birthdayYear);
        Contact contact = user.getContact();
        contact.setFirstName(firstName);
        contact.setMiddleName(middleName);
        contact.setLastName(lastName);
        contact.setPrefixId(prefixId);
        contact.setSuffixId(suffixId);
        contact.setMale(male);
        contact.setBirthday(birthday);
        contact.setJobTitle(jobTitle);
        contactPersistence.update(contact, serviceContext);
        // Indexer
        Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
        indexer.reindex(user);
    }
    user.setStatus(WorkflowConstants.STATUS_DRAFT);
    userPersistence.update(user, serviceContext);
    // Workflow
    long workflowUserId = creatorUserId;
    if (workflowUserId == user.getUserId()) {
        workflowUserId = defaultUser.getUserId();
    }
    ServiceContext workflowServiceContext = serviceContext;
    if (workflowServiceContext == null) {
        workflowServiceContext = new ServiceContext();
    }
    workflowServiceContext.setAttribute("autoPassword", autoPassword);
    workflowServiceContext.setAttribute("passwordUnencrypted", password1);
    workflowServiceContext.setAttribute("sendEmail", sendEmail);
    WorkflowHandlerRegistryUtil.startWorkflowInstance(companyId, workflowUserId, User.class.getName(), user.getUserId(), user, workflowServiceContext);
    return getUserByEmailAddress(companyId, emailAddress);
}
Also used : User(com.liferay.portal.model.User) ScreenNameGenerator(com.liferay.portal.security.auth.ScreenNameGenerator) ServiceContext(com.liferay.portal.service.ServiceContext) ContactFirstNameException(com.liferay.portal.ContactFirstNameException) ModelListenerException(com.liferay.portal.ModelListenerException) NoSuchImageException(com.liferay.portal.NoSuchImageException) GroupFriendlyURLException(com.liferay.portal.GroupFriendlyURLException) DuplicateOpenIdException(com.liferay.portal.DuplicateOpenIdException) ImageSizeException(com.liferay.portlet.documentlibrary.ImageSizeException) PasswordExpiredException(com.liferay.portal.PasswordExpiredException) UserPasswordException(com.liferay.portal.UserPasswordException) NoSuchUserException(com.liferay.portal.NoSuchUserException) UserSmsException(com.liferay.portal.UserSmsException) NoSuchRoleException(com.liferay.portal.NoSuchRoleException) PortalException(com.liferay.portal.kernel.exception.PortalException) UserIdException(com.liferay.portal.UserIdException) UserPortraitTypeException(com.liferay.portal.UserPortraitTypeException) RequiredUserException(com.liferay.portal.RequiredUserException) ReservedUserScreenNameException(com.liferay.portal.ReservedUserScreenNameException) IOException(java.io.IOException) ContactBirthdayException(com.liferay.portal.ContactBirthdayException) UserReminderQueryException(com.liferay.portal.UserReminderQueryException) DuplicateUserScreenNameException(com.liferay.portal.DuplicateUserScreenNameException) UserEmailAddressException(com.liferay.portal.UserEmailAddressException) ContactFullNameException(com.liferay.portal.ContactFullNameException) EncryptorException(com.liferay.util.EncryptorException) CompanyMaxUsersException(com.liferay.portal.CompanyMaxUsersException) NoSuchTicketException(com.liferay.portal.NoSuchTicketException) UserScreenNameException(com.liferay.portal.UserScreenNameException) ContactLastNameException(com.liferay.portal.ContactLastNameException) ReservedUserEmailAddressException(com.liferay.portal.ReservedUserEmailAddressException) DuplicateUserEmailAddressException(com.liferay.portal.DuplicateUserEmailAddressException) NoSuchUserGroupException(com.liferay.portal.NoSuchUserGroupException) PrincipalException(com.liferay.portal.security.auth.PrincipalException) SystemException(com.liferay.portal.kernel.exception.SystemException) NoSuchOrganizationException(com.liferay.portal.NoSuchOrganizationException) UserLockoutException(com.liferay.portal.UserLockoutException) UserPortraitSizeException(com.liferay.portal.UserPortraitSizeException) Date(java.util.Date) Contact(com.liferay.portal.model.Contact) Indexer(com.liferay.portal.kernel.search.Indexer) SystemException(com.liferay.portal.kernel.exception.SystemException) UserPasswordException(com.liferay.portal.UserPasswordException) FullNameGenerator(com.liferay.portal.security.auth.FullNameGenerator) PasswordPolicy(com.liferay.portal.model.PasswordPolicy) PortalException(com.liferay.portal.kernel.exception.PortalException)

Example 37 with ServiceContext

use of com.liferay.portal.service.ServiceContext in project liferay-ide by liferay.

the class SongLocalServiceImpl method moveSongToTrash.

@Indexable(type = IndexableType.REINDEX)
public Song moveSongToTrash(long userId, Song song) throws PortalException, SystemException {
    ServiceContext serviceContext = new ServiceContext();
    // Entry
    User user = userPersistence.findByPrimaryKey(userId);
    Date now = new Date();
    int oldStatus = song.getStatus();
    song.setModifiedDate(serviceContext.getModifiedDate(now));
    song.setStatus(WorkflowConstants.STATUS_IN_TRASH);
    song.setStatusByUserId(user.getUserId());
    song.setStatusByUserName(user.getFullName());
    song.setStatusDate(serviceContext.getModifiedDate(now));
    // Asset
    assetEntryLocalService.updateVisible(Song.class.getName(), song.getSongId(), false);
    // Trash
    UnicodeProperties typeSettingsProperties = new UnicodeProperties();
    typeSettingsProperties.put("title", song.getName());
    TrashEntry trashEntry = trashEntryLocalService.addTrashEntry(userId, song.getGroupId(), Song.class.getName(), song.getSongId(), song.getUuid(), null, oldStatus, null, typeSettingsProperties);
    song.setName(TrashUtil.getTrashTitle(trashEntry.getEntryId()));
    songPersistence.update(song);
    return song;
}
Also used : Song(org.liferay.jukebox.model.Song) User(com.liferay.portal.model.User) TrashEntry(com.liferay.portlet.trash.model.TrashEntry) UnicodeProperties(com.liferay.portal.kernel.util.UnicodeProperties) ServiceContext(com.liferay.portal.service.ServiceContext) Date(java.util.Date) Indexable(com.liferay.portal.kernel.search.Indexable)

Example 38 with ServiceContext

use of com.liferay.portal.service.ServiceContext in project liferay-ide by liferay.

the class SongLocalServiceImpl method restoreSongFromTrash.

@Indexable(type = IndexableType.REINDEX)
@Override
public Song restoreSongFromTrash(long userId, long songId) throws PortalException, SystemException {
    ServiceContext serviceContext = new ServiceContext();
    // Entry
    User user = userPersistence.findByPrimaryKey(userId);
    Date now = new Date();
    TrashEntry trashEntry = trashEntryLocalService.getEntry(Song.class.getName(), songId);
    Song song = songPersistence.findByPrimaryKey(songId);
    song.setName(TrashUtil.getOriginalTitle(song.getName()));
    song.setModifiedDate(serviceContext.getModifiedDate(now));
    song.setStatus(trashEntry.getStatus());
    song.setStatusByUserId(user.getUserId());
    song.setStatusByUserName(user.getFullName());
    song.setStatusDate(serviceContext.getModifiedDate(now));
    songPersistence.update(song);
    assetEntryLocalService.updateVisible(Song.class.getName(), song.getSongId(), true);
    trashEntryLocalService.deleteEntry(Song.class.getName(), songId);
    return song;
}
Also used : Song(org.liferay.jukebox.model.Song) User(com.liferay.portal.model.User) TrashEntry(com.liferay.portlet.trash.model.TrashEntry) ServiceContext(com.liferay.portal.service.ServiceContext) Date(java.util.Date) Indexable(com.liferay.portal.kernel.search.Indexable)

Example 39 with ServiceContext

use of com.liferay.portal.service.ServiceContext in project liferay-ide by liferay.

the class MediaWikiImporter method importPage.

protected void importPage(long userId, String author, WikiNode node, String title, String content, String summary, Map<String, String> usersMap, boolean strictImportMode) throws PortalException {
    try {
        long authorUserId = getUserId(userId, node, author, usersMap);
        String parentTitle = readParentTitle(content);
        String redirectTitle = readRedirectTitle(content);
        ServiceContext serviceContext = new ServiceContext();
        serviceContext.setAddGroupPermissions(true);
        serviceContext.setAddGuestPermissions(true);
        serviceContext.setAssetTagNames(readAssetTagNames(userId, node, content));
        if (Validator.isNull(redirectTitle)) {
            _translator.setStrictImportMode(strictImportMode);
            content = _translator.translate(content);
        } else {
            content = StringPool.DOUBLE_OPEN_BRACKET + redirectTitle + StringPool.DOUBLE_CLOSE_BRACKET;
        }
        WikiPage page = null;
        try {
            page = WikiPageLocalServiceUtil.getPage(node.getNodeId(), title);
        } catch (NoSuchPageException nspe) {
            page = WikiPageLocalServiceUtil.addPage(authorUserId, node.getNodeId(), title, WikiPageConstants.NEW, null, true, serviceContext);
        }
        WikiPageLocalServiceUtil.updatePage(authorUserId, node.getNodeId(), title, page.getVersion(), content, summary, true, "creole", parentTitle, redirectTitle, serviceContext);
    } catch (Exception e) {
        throw new PortalException("Error importing page " + title, e);
    }
}
Also used : NoSuchPageException(com.liferay.portlet.wiki.NoSuchPageException) ServiceContext(com.liferay.portal.service.ServiceContext) WikiPage(com.liferay.portlet.wiki.model.WikiPage) PortalException(com.liferay.portal.kernel.exception.PortalException) NoSuchPageException(com.liferay.portlet.wiki.NoSuchPageException) ImportFilesException(com.liferay.portlet.wiki.ImportFilesException) PortalException(com.liferay.portal.kernel.exception.PortalException) SystemException(com.liferay.portal.kernel.exception.SystemException) DocumentException(com.liferay.portal.kernel.xml.DocumentException) IOException(java.io.IOException) NoSuchTagException(com.liferay.portlet.asset.NoSuchTagException)

Example 40 with ServiceContext

use of com.liferay.portal.service.ServiceContext in project liferay-ide by liferay.

the class MediaWikiImporter method processSpecialPages.

protected void processSpecialPages(long userId, WikiNode node, Element rootElement, List<String> specialNamespaces) throws PortalException {
    ProgressTracker progressTracker = ProgressTrackerThreadLocal.getProgressTracker();
    List<Element> pageElements = rootElement.elements("page");
    for (int i = 0; i < pageElements.size(); i++) {
        Element pageElement = pageElements.get(i);
        String title = pageElement.elementText("title");
        if (!title.startsWith("Category:")) {
            if (isSpecialMediaWikiPage(title, specialNamespaces)) {
                rootElement.remove(pageElement);
            }
            continue;
        }
        String categoryName = title.substring("Category:".length());
        categoryName = normalize(categoryName, 75);
        Element revisionElement = pageElement.element("revision");
        String description = revisionElement.elementText("text");
        description = normalizeDescription(description);
        try {
            AssetTag assetTag = null;
            try {
                assetTag = AssetTagLocalServiceUtil.getTag(node.getGroupId(), categoryName);
            } catch (NoSuchTagException nste) {
                ServiceContext serviceContext = new ServiceContext();
                serviceContext.setAddGroupPermissions(true);
                serviceContext.setAddGuestPermissions(true);
                serviceContext.setScopeGroupId(node.getGroupId());
                assetTag = AssetTagLocalServiceUtil.addTag(userId, categoryName, null, serviceContext);
                if (PropsValues.ASSET_TAG_PROPERTIES_ENABLED && Validator.isNotNull(description)) {
                    AssetTagPropertyLocalServiceUtil.addTagProperty(userId, assetTag.getTagId(), "description", description);
                }
            }
        } catch (SystemException se) {
            _log.error(se, se);
        }
        if ((i % 5) == 0) {
            progressTracker.setPercent((i * 10) / pageElements.size());
        }
    }
}
Also used : AssetTag(com.liferay.portlet.asset.model.AssetTag) NoSuchTagException(com.liferay.portlet.asset.NoSuchTagException) SystemException(com.liferay.portal.kernel.exception.SystemException) ProgressTracker(com.liferay.portal.kernel.util.ProgressTracker) ServiceContext(com.liferay.portal.service.ServiceContext) Element(com.liferay.portal.kernel.xml.Element)

Aggregations

ServiceContext (com.liferay.portal.service.ServiceContext)57 User (com.liferay.portal.model.User)11 PrincipalException (com.liferay.portal.security.auth.PrincipalException)11 InputStream (java.io.InputStream)11 AlbumNameException (org.liferay.jukebox.AlbumNameException)9 ArtistNameException (org.liferay.jukebox.ArtistNameException)9 DuplicatedSongException (org.liferay.jukebox.DuplicatedSongException)9 SongNameException (org.liferay.jukebox.SongNameException)9 UploadPortletRequest (com.liferay.portal.kernel.upload.UploadPortletRequest)8 Song (org.liferay.jukebox.model.Song)8 ThemeDisplay (com.liferay.portal.theme.ThemeDisplay)7 Date (java.util.Date)7 SystemException (com.liferay.portal.kernel.exception.SystemException)6 FileEntry (com.liferay.portal.kernel.repository.model.FileEntry)6 PortalException (com.liferay.portal.kernel.exception.PortalException)5 JSONObject (com.liferay.portal.kernel.json.JSONObject)5 Artist (org.liferay.jukebox.model.Artist)5 Gadget (com.liferay.opensocial.model.Gadget)4 Folder (com.liferay.portal.kernel.repository.model.Folder)4 Element (com.liferay.portal.kernel.xml.Element)4