Example usage for com.liferay.portal.kernel.theme ThemeDisplay getUserId

List of usage examples for com.liferay.portal.kernel.theme ThemeDisplay getUserId

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.theme ThemeDisplay getUserId.

Prototype

public long getUserId() 

Source Link

Document

Returns the ID of the user for which the current request is being handled.

Usage

From source file:com.liferay.comment.taglib.internal.struts.EditDiscussionStrutsAction.java

License:Open Source License

protected void subscribeToComments(HttpServletRequest request, boolean subscribe) throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

    String className = ParamUtil.getString(request, "className");
    long classPK = ParamUtil.getLong(request, "classPK");

    if (subscribe) {
        _commentManager.subscribeDiscussion(themeDisplay.getUserId(), themeDisplay.getScopeGroupId(), className,
                classPK);//from  w  w  w .  ja v a  2  s  . c  o m
    } else {
        _commentManager.unsubscribeDiscussion(themeDisplay.getUserId(), className, classPK);
    }
}

From source file:com.liferay.comment.taglib.internal.struts.EditDiscussionStrutsAction.java

License:Open Source License

protected long updateComment(HttpServletRequest request) throws Exception {
    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

    long commentId = ParamUtil.getLong(request, "commentId");

    String className = ParamUtil.getString(request, "className");
    long classPK = ParamUtil.getLong(request, "classPK");
    long parentCommentId = ParamUtil.getLong(request, "parentCommentId");
    String subject = ParamUtil.getString(request, "subject");
    String body = ParamUtil.getString(request, "body");

    Function<String, ServiceContext> serviceContextFunction = new ServiceContextFunction(request);

    DiscussionPermission discussionPermission = _getDiscussionPermission(themeDisplay);

    if (commentId <= 0) {

        // Add message

        User user = null;//from w  w  w  .  j a v  a2s .  c  om

        if (themeDisplay.isSignedIn()) {
            user = themeDisplay.getUser();
        } else {
            String emailAddress = ParamUtil.getString(request, "emailAddress");

            user = _userLocalService.fetchUserByEmailAddress(themeDisplay.getCompanyId(), emailAddress);

            if ((user == null) || (user.getStatus() != WorkflowConstants.STATUS_INCOMPLETE)) {

                return 0;
            }
        }

        String name = PrincipalThreadLocal.getName();

        PrincipalThreadLocal.setName(user.getUserId());

        try {
            discussionPermission.checkAddPermission(themeDisplay.getCompanyId(), themeDisplay.getScopeGroupId(),
                    className, classPK);

            commentId = _commentManager.addComment(user.getUserId(), className, classPK, user.getFullName(),
                    parentCommentId, subject, body, serviceContextFunction);
        } finally {
            PrincipalThreadLocal.setName(name);
        }
    } else {

        // Update message

        if (Validator.isNull(className) || (classPK == 0)) {
            Comment comment = _commentManager.fetchComment(commentId);

            if (comment != null) {
                className = comment.getClassName();
                classPK = comment.getClassPK();
            }
        }

        discussionPermission.checkUpdatePermission(commentId);

        commentId = _commentManager.updateComment(themeDisplay.getUserId(), className, classPK, commentId,
                subject, body, serviceContextFunction);
    }

    // Subscription

    boolean subscribe = ParamUtil.getBoolean(request, "subscribe");

    if (subscribe) {
        _commentManager.subscribeDiscussion(themeDisplay.getUserId(), themeDisplay.getScopeGroupId(), className,
                classPK);
    }

    return commentId;
}

From source file:com.liferay.contacts.web.internal.portlet.ContactsCenterPortlet.java

License:Open Source License

public void addSocialRelation(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    long[] userIds = getUserIds(actionRequest);

    int type = ParamUtil.getInteger(actionRequest, "type");

    if (type == SocialRelationConstants.TYPE_BI_CONNECTION) {
        return;//from  w w  w  .  j  av  a 2s  . c o m
    }

    for (long userId : userIds) {
        if (userId == themeDisplay.getUserId()) {
            continue;
        }

        boolean blocked = socialRelationLocalService.hasRelation(userId, themeDisplay.getUserId(),
                SocialRelationConstants.TYPE_UNI_ENEMY);

        if (type == SocialRelationConstants.TYPE_UNI_ENEMY) {
            socialRelationLocalService.deleteRelations(themeDisplay.getUserId(), userId);

            socialRelationLocalService.deleteRelations(userId, themeDisplay.getUserId());
        } else if (blocked) {
            continue;
        }

        socialRelationLocalService.addRelation(themeDisplay.getUserId(), userId, type);

        if (blocked) {
            socialRelationLocalService.addRelation(userId, themeDisplay.getUserId(), type);
        }
    }
}

From source file:com.liferay.contacts.web.internal.portlet.ContactsCenterPortlet.java

License:Open Source License

public void deleteSocialRelation(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    long[] userIds = getUserIds(actionRequest);

    int type = ParamUtil.getInteger(actionRequest, "type");

    for (long userId : userIds) {
        if (userId == themeDisplay.getUserId()) {
            continue;
        }//from   www  .  j a v  a  2  s .  c o m

        try {
            socialRelationLocalService.deleteRelation(themeDisplay.getUserId(), userId, type);
        } catch (NoSuchRelationException nsre) {

            // LPS-52675

            if (_log.isDebugEnabled()) {
                _log.debug(nsre, nsre);
            }
        }
    }
}

From source file:com.liferay.contacts.web.internal.portlet.ContactsCenterPortlet.java

License:Open Source License

public void requestSocialRelation(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    long[] userIds = getUserIds(actionRequest);

    int type = ParamUtil.getInteger(actionRequest, "type");

    for (long userId : userIds) {
        if (userId == themeDisplay.getUserId()) {
            continue;
        }/*from ww w.j a v a 2  s  .  c o  m*/

        if (socialRelationLocalService.hasRelation(userId, themeDisplay.getUserId(),
                SocialRelationConstants.TYPE_BI_CONNECTION)
                || socialRelationLocalService.hasRelation(userId, themeDisplay.getUserId(),
                        SocialRelationConstants.TYPE_UNI_ENEMY)
                || socialRequestLocalService.hasRequest(themeDisplay.getUserId(), User.class.getName(),
                        themeDisplay.getUserId(), type, userId, SocialRequestConstants.STATUS_PENDING)) {

            continue;
        }

        JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();

        String portletId = portal.getPortletId(actionRequest);

        extraDataJSONObject.put("portletId", PortletIdCodec.decodePortletName(portletId));

        SocialRequest socialRequest = socialRequestLocalService.addRequest(themeDisplay.getUserId(), 0,
                User.class.getName(), themeDisplay.getUserId(), type, extraDataJSONObject.toString(), userId);

        sendNotificationEvent(socialRequest);
    }
}

From source file:com.liferay.contacts.web.internal.portlet.ContactsCenterPortlet.java

License:Open Source License

public void updateEntry(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    String redirect = ParamUtil.getString(actionRequest, "redirect");

    long entryId = ParamUtil.getLong(actionRequest, "entryId");

    String fullName = ParamUtil.getString(actionRequest, "fullName");
    String emailAddress = ParamUtil.getString(actionRequest, "emailAddress");
    String comments = ParamUtil.getString(actionRequest, "comments");

    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

    String message = null;/* w w w. j a v  a  2s  .c o m*/

    try {
        Entry entry = null;

        if (entryId > 0) {
            entry = entryLocalService.getEntry(entryId);

            if (entry.getUserId() == themeDisplay.getUserId()) {
                entry = entryLocalService.updateEntry(entryId, fullName, emailAddress, comments);

                message = "you-have-successfully-updated-the-contact";
            }
        } else {
            entry = entryLocalService.addEntry(themeDisplay.getUserId(), fullName, emailAddress, comments);

            message = "you-have-successfully-added-a-new-contact";
        }

        jsonObject.put("contact", getEntryJSONObject(actionResponse, themeDisplay, entry, redirect));

        JSONObject contactsJSONObject = getContactsJSONObject(actionRequest, actionResponse);

        jsonObject.put("contactList", contactsJSONObject);

        jsonObject.put("success", Boolean.TRUE);
    } catch (Exception e) {
        if (e instanceof ContactFullNameException) {
            message = "full-name-cannot-be-empty";
        } else if (e instanceof DuplicateEntryEmailAddressException) {
            message = "there-is-already-a-contact-with-this-email-address";
        } else if (e instanceof EntryEmailAddressException) {
            message = "please-enter-a-valid-email-address";
        } else {
            message = "an-error-occurred-while-processing-the-requested-resource";
        }

        jsonObject.put("success", Boolean.FALSE);
    }

    jsonObject.put("message", translate(actionRequest, message));

    writeJSON(actionRequest, actionResponse, jsonObject);
}

From source file:com.liferay.contacts.web.internal.portlet.ContactsCenterPortlet.java

License:Open Source License

protected void deleteEntry(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    long entryId = ParamUtil.getLong(actionRequest, "entryId");

    if (entryId > 0) {
        Entry entry = entryLocalService.getEntry(entryId);

        if (entry.getUserId() == themeDisplay.getUserId()) {
            entryLocalService.deleteEntry(entryId);
        }/*from w w w.j  av a 2s  . co  m*/
    }
}

From source file:com.liferay.contacts.web.internal.portlet.ContactsCenterPortlet.java

License:Open Source License

protected JSONObject getContactsJSONObject(PortletRequest portletRequest, PortletResponse portletResponse)
        throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) portletRequest.getAttribute(WebKeys.THEME_DISPLAY);

    String redirect = ParamUtil.getString(portletRequest, "redirect");

    String filterBy = ParamUtil.getString(portletRequest, "filterBy");
    String keywords = ParamUtil.getString(portletRequest, "keywords");
    int start = ParamUtil.getInteger(portletRequest, "start");
    int end = ParamUtil.getInteger(portletRequest, "end");

    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

    JSONObject optionsJSONObject = JSONFactoryUtil.createJSONObject();

    optionsJSONObject.put("end", end);
    optionsJSONObject.put("filterBy", filterBy);
    optionsJSONObject.put("keywords", keywords);
    optionsJSONObject.put("start", start);

    jsonObject.put("options", optionsJSONObject);

    PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();

    String portletId = portletDisplay.getId();

    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

    if (filterBy.equals(ContactsConstants.FILTER_BY_DEFAULT)
            && !portletId.equals(ContactsPortletKeys.MEMBERS)) {

        List<BaseModel<?>> contacts = entryLocalService.searchUsersAndContacts(themeDisplay.getCompanyId(),
                themeDisplay.getUserId(), keywords, start, end);

        int contactsCount = entryLocalService.searchUsersAndContactsCount(themeDisplay.getCompanyId(),
                themeDisplay.getUserId(), keywords);

        jsonObject.put("count", contactsCount);

        for (BaseModel<?> contact : contacts) {
            JSONObject contactJSONObject = null;

            if (contact instanceof User) {
                contactJSONObject = getUserJSONObject(portletResponse, themeDisplay, (User) contact);
            } else {
                contactJSONObject = getEntryJSONObject(portletResponse, themeDisplay, (Entry) contact,
                        redirect);//from   w w w .j  a va  2 s .co  m
            }

            jsonArray.put(contactJSONObject);
        }
    } else if (filterBy.equals(ContactsConstants.FILTER_BY_FOLLOWERS)
            && !portletId.equals(ContactsPortletKeys.MEMBERS)) {

        List<SocialRelation> socialRelations = socialRelationLocalService.getInverseRelations(
                themeDisplay.getUserId(), SocialRelationConstants.TYPE_UNI_FOLLOWER, start, end);

        for (SocialRelation socialRelation : socialRelations) {
            jsonArray.put(getUserJSONObject(portletResponse, themeDisplay, socialRelation.getUserId1()));
        }
    } else if (filterBy.equals(ContactsConstants.FILTER_BY_TYPE_MY_CONTACTS)
            && !portletId.equals(ContactsPortletKeys.MEMBERS)) {

        List<Entry> entries = entryLocalService.search(themeDisplay.getUserId(), keywords, start, end);

        int entriesCount = entryLocalService.searchCount(themeDisplay.getUserId(), keywords);

        jsonObject.put("count", entriesCount);

        for (Entry entry : entries) {
            JSONObject contactJSONObject = getEntryJSONObject(portletResponse, themeDisplay, entry, redirect);

            jsonArray.put(contactJSONObject);
        }
    } else {
        LinkedHashMap<String, Object> params = new LinkedHashMap<>();

        params.put("inherit", Boolean.TRUE);

        Group group = themeDisplay.getScopeGroup();
        Layout layout = themeDisplay.getLayout();

        if (group.isUser() && layout.isPublicLayout()) {
            params.put("socialRelationType",
                    new Long[] { group.getClassPK(), (long) SocialRelationConstants.TYPE_BI_CONNECTION });
        } else if (filterBy.startsWith(ContactsConstants.FILTER_BY_TYPE)) {
            params.put("socialRelationType",
                    new Long[] { themeDisplay.getUserId(), ContactsUtil.getSocialRelationType(filterBy) });
        }

        if (portletId.equals(ContactsPortletKeys.MEMBERS)) {
            params.put("usersGroups", group.getGroupId());
        } else if (filterBy.startsWith(ContactsConstants.FILTER_BY_GROUP)) {
            params.put("usersGroups", ContactsUtil.getGroupId(filterBy));
        }

        List<User> usersList = null;

        if (filterBy.equals(ContactsConstants.FILTER_BY_ADMINS)) {
            Role siteAdministratorRole = roleLocalService.getRole(group.getCompanyId(),
                    RoleConstants.SITE_ADMINISTRATOR);

            params.put("userGroupRole", new Long[] { group.getGroupId(), siteAdministratorRole.getRoleId() });

            Set<User> users = new HashSet<>();

            users.addAll(userLocalService.search(themeDisplay.getCompanyId(), keywords,
                    WorkflowConstants.STATUS_APPROVED, params, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
                    (OrderByComparator) null));

            Role siteOwnerRole = roleLocalService.getRole(group.getCompanyId(), RoleConstants.SITE_OWNER);

            params.put("userGroupRole", new Long[] { group.getGroupId(), siteOwnerRole.getRoleId() });

            users.addAll(userLocalService.search(themeDisplay.getCompanyId(), keywords,
                    WorkflowConstants.STATUS_APPROVED, params, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
                    (OrderByComparator) null));

            usersList = new ArrayList<>(users);

            ListUtil.sort(usersList, new UserLastNameComparator(true));
        } else {
            int usersCount = userLocalService.searchCount(themeDisplay.getCompanyId(), keywords,
                    WorkflowConstants.STATUS_APPROVED, params);

            jsonObject.put("count", usersCount);

            usersList = userLocalService.search(themeDisplay.getCompanyId(), keywords,
                    WorkflowConstants.STATUS_APPROVED, params, start, end, new UserLastNameComparator(true));
        }

        for (User user : usersList) {
            JSONObject userJSONObject = getUserJSONObject(portletResponse, themeDisplay, user);

            jsonArray.put(userJSONObject);
        }
    }

    jsonObject.put("users", jsonArray);

    return jsonObject;
}

From source file:com.liferay.contacts.web.internal.portlet.ContactsCenterPortlet.java

License:Open Source License

protected JSONObject getUserJSONObject(PortletResponse portletResponse, ThemeDisplay themeDisplay, User user)
        throws Exception {

    user = user.toEscapedModel();/*from   ww  w  .  j  a  va2s .co  m*/

    JSONObject jsonObject = ContactsUtil.getUserJSONObject(themeDisplay.getUserId(), user);

    jsonObject.put("portraitURL", user.getPortraitURL(themeDisplay));

    LiferayPortletResponse liferayPortletResponse = portal.getLiferayPortletResponse(portletResponse);

    PortletURL viewSummaryURL = liferayPortletResponse.createRenderURL();

    viewSummaryURL.setParameter("mvcPath", "/contacts_center/view_resources.jsp");
    viewSummaryURL.setParameter("userId", String.valueOf(user.getUserId()));
    viewSummaryURL.setParameter("portalUser", Boolean.TRUE.toString());
    viewSummaryURL.setWindowState(LiferayWindowState.EXCLUSIVE);

    jsonObject.put("viewSummaryURL", viewSummaryURL.toString());

    return jsonObject;
}

From source file:com.liferay.document.library.web.internal.portlet.action.EditFileEntryMVCActionCommand.java

License:Open Source License

protected void addMultipleFileEntries(PortletConfig portletConfig, ActionRequest actionRequest,
        ActionResponse actionResponse, String selectedFileName, List<KeyValuePair> validFileNameKVPs,
        List<KeyValuePair> invalidFileNameKVPs) throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    long repositoryId = ParamUtil.getLong(actionRequest, "repositoryId");
    long folderId = ParamUtil.getLong(actionRequest, "folderId");
    String description = ParamUtil.getString(actionRequest, "description");
    String changeLog = ParamUtil.getString(actionRequest, "changeLog");

    FileEntry tempFileEntry = null;/*  ww w.  j a  v a 2  s.co m*/

    try {
        tempFileEntry = TempFileEntryUtil.getTempFileEntry(themeDisplay.getScopeGroupId(),
                themeDisplay.getUserId(), TEMP_FOLDER_NAME, selectedFileName);

        String originalSelectedFileName = TempFileEntryUtil
                .getOriginalTempFileName(tempFileEntry.getFileName());

        String uniqueFileName = DLUtil.getUniqueFileName(tempFileEntry.getGroupId(), folderId,
                originalSelectedFileName);

        String mimeType = tempFileEntry.getMimeType();
        InputStream inputStream = tempFileEntry.getContentStream();
        long size = tempFileEntry.getSize();

        ServiceContext serviceContext = ServiceContextFactory.getInstance(DLFileEntry.class.getName(),
                actionRequest);

        _dlAppService.addFileEntry(repositoryId, folderId, uniqueFileName, mimeType, uniqueFileName,
                description, changeLog, inputStream, size, serviceContext);

        validFileNameKVPs.add(new KeyValuePair(uniqueFileName, selectedFileName));

        return;
    } catch (Exception e) {
        String errorMessage = getAddMultipleFileEntriesErrorMessage(portletConfig, actionRequest,
                actionResponse, e);

        invalidFileNameKVPs.add(new KeyValuePair(selectedFileName, errorMessage));
    } finally {
        if (tempFileEntry != null) {
            TempFileEntryUtil.deleteTempFileEntry(tempFileEntry.getFileEntryId());
        }
    }
}