Example usage for com.liferay.portal.kernel.service UserLocalServiceUtil fetchUser

List of usage examples for com.liferay.portal.kernel.service UserLocalServiceUtil fetchUser

Introduction

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

Prototype

public static com.liferay.portal.kernel.model.User fetchUser(long userId) 

Source Link

Usage

From source file:com.liferay.calendar.notification.impl.NotificationUtil.java

License:Open Source License

private static List<NotificationRecipient> _getNotificationRecipients(CalendarBooking calendarBooking)
        throws Exception {

    List<NotificationRecipient> notificationRecipients = new ArrayList<>();

    CalendarResource calendarResource = calendarBooking.getCalendarResource();

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

    if (calendarBooking.isMasterBooking()) {
        users.add(UserLocalServiceUtil.fetchUser(calendarBooking.getUserId()));
    }/*from  www .j a  v a2  s.com*/

    users.add(UserLocalServiceUtil.fetchUser(calendarResource.getUserId()));

    for (User user : users) {
        if (user == null) {
            continue;
        }

        if (!user.isActive()) {
            if (_log.isDebugEnabled()) {
                _log.debug("Skip inactive user " + user.getUserId());
            }

            continue;
        }

        notificationRecipients.add(new NotificationRecipient(user));
    }

    return notificationRecipients;
}

From source file:com.liferay.document.library.document.conversion.internal.ImageRequestTokenUtil.java

License:Open Source License

public static long getUserId(String tokenString) {
    try {//from   ww w .j av  a 2  s.c  om
        JsonTokenParser jsonTokenParser = getJsonTokenParser();

        JsonToken jsonToken = jsonTokenParser.verifyAndDeserialize(tokenString);

        JsonPrimitive userIdJsonPrimitive = jsonToken.getParamAsPrimitive("userId");

        if (userIdJsonPrimitive == null) {
            if (_log.isDebugEnabled()) {
                _log.debug("Unable to find \"userId\" parameter in payload " + tokenString);
            }

            return 0;
        }

        long userId = userIdJsonPrimitive.getAsLong();

        User user = UserLocalServiceUtil.fetchUser(userId);

        Date passwordModifiedDate = user.getPasswordModifiedDate();

        if (passwordModifiedDate != null) {
            Instant instant = jsonToken.getIssuedAt();

            if (instant.isBefore(passwordModifiedDate.getTime())) {
                if (_log.isDebugEnabled()) {
                    _log.debug("Unable to accept token because the password was " + "changed");
                }

                return 0;
            }
        }

        return userId;
    } catch (Exception e) {
        if (_log.isDebugEnabled()) {
            _log.debug("Unable to parse and verify token " + tokenString, e);
        }

        return 0;
    }
}

From source file:com.liferay.frontend.taglib.servlet.taglib.UserVerticalCardTag.java

License:Open Source License

protected User getUser() {
    return UserLocalServiceUtil.fetchUser(_userId);
}

From source file:com.liferay.message.boards.comment.internal.MBCommentImpl.java

License:Open Source License

@Override
public User getUser() {
    return UserLocalServiceUtil.fetchUser(getUserId());
}

From source file:com.liferay.tool.datamanipulator.handler.BaseHandler.java

License:Open Source License

private Object[] _getArgs(String[] argNames, Class<?>[] argClazzs, RequestContext requestContext,
        Map<String, Object> entrySpecifiedArgs) throws Exception {

    long companyId = requestContext.getCompanyId();
    long groupId = requestContext.getGroupId();
    long userId = requestContext.getUserId();

    Date now = new Date();

    ServiceContext serviceContext = HandlerUtil.getServiceContext(groupId, userId);

    Map<String, Calendar> dateMap = new HashMap<String, Calendar>(entryDateFields.size());

    for (String entryDateField : entryDateFields) {
        String dateVarName = entryDateField + "Date";
        String dateKeyName = entryDateField + "-date";

        Date serviceContextDate = (Date) BeanUtil.getPropertySilently(serviceContext, dateVarName);

        Calendar dateVarValue;/* www .  ja v a  2 s  . co m*/
        if (requestContext.contains(dateKeyName + "-from") && requestContext.contains(dateKeyName + "-to")) {

            dateVarValue = requestContext.getBetweenCalendar(dateKeyName);
        } else if (Validator.isNotNull(serviceContextDate)) {
            dateVarValue = Calendar.getInstance();

            dateVarValue.setTime(serviceContextDate);
        } else {
            dateVarValue = Calendar.getInstance();

            dateVarValue.setTime(now);
        }

        dateMap.put(dateVarName, dateVarValue);
    }

    StringBuilder sb = new StringBuilder();
    sb.append(_entryName);
    sb.append(requestContext.getString("entryCount"));
    sb.append(" ${fieldName} ");

    if (requestContext.contains("editCount")) {
        sb.append("Edited ");
        sb.append(requestContext.getString("editCount"));
        sb.append(" times ");
    }

    sb.append(requestContext.getRandomString());

    String entryTemplate = sb.toString();

    User user = UserLocalServiceUtil.fetchUser(userId);

    List<Object> argValues = new ArrayList<Object>(argNames.length);

    for (int i = 0; i < argNames.length; i++) {
        String argName = argNames[i];
        Class<?> argClazz = argClazzs[i];

        if (entrySpecifiedArgs.containsKey(argName)) {
            argValues.add(entrySpecifiedArgs.get(argName));
        } else if (argName.equals(Field.COMPANY_ID)) {
            argValues.add(companyId);
        } else if (argName.matches(".*Date.*")) {
            int x = argName.indexOf("Date") + 4;

            String dateKey = argName.substring(0, x);

            Calendar calendar = dateMap.get(dateKey);

            String calendarFieldName = argName.substring(x).toUpperCase();
            if (calendarFieldName.equals("DAY")) {
                calendarFieldName = "DATE";
            }

            int calendarFieldValue = (Integer) GetterUtil.getFieldValue(Calendar.class.getName(),
                    calendarFieldName);

            argValues.add(calendar.get(calendarFieldValue));
        } else if (entryStringFields.contains(argName)) {
            Map<String, String> context = new HashMap<String, String>(1);
            context.put("fieldName", argName);

            String content = StringUtil.getStringFieldValue(argName, context, entryTemplate);

            argValues.add(content);
        } else if (entryMapFields.contains(argName)) {
            argName = argName.substring(0, (argName.length() - 3));

            Map<String, String> context = new HashMap<String, String>(1);
            context.put("fieldName", argName);

            String content = StringUtil.getStringFieldValue(argName, context, entryTemplate);

            argValues.add(StringUtil.getLocalizationMap(content));
        } else if (argName.equals("friendlyURL")) {
            Map<String, String> context = new HashMap<String, String>(1);
            context.put("fieldName", "name");

            String friendlyURL = StringUtil.getStringFieldValue(argName, context, entryTemplate);

            friendlyURL = StringPool.SLASH + FriendlyURLNormalizerUtil.normalize(friendlyURL);

            argValues.add(friendlyURL);
        } else if (argName.equals(Field.GROUP_ID)) {
            argValues.add(groupId);
        } else if (argName.equals(getParentClassPKName())) {
            argValues.add(Long.valueOf(_getParentClassPK(requestContext)));
        } else if (argName.equals("locale")) {
            argValues.add(LocaleUtil.getDefault());
        } else if (argName.equals("serviceContext")) {
            argValues.add(serviceContext);
        } else if (argName.equals(Field.USER_ID)) {
            argValues.add(userId);
        } else if (argName.equals(Field.USER_NAME)) {
            argValues.add(user.getFullName());
        } else if (requestContext.contains(argName)) {
            argValues.add(requestContext.get(argName));
        } else {
            Object argValue = null;
            try {
                Object object = argClazz.newInstance();

                if (object instanceof String) {
                    argValue = StringPool.BLANK;
                }
            } catch (InstantiationException e) {
                Type type = argClazz;

                if (type.equals(Boolean.TYPE)) {
                    argValue = false;
                } else if (type.equals(Integer.TYPE)) {
                    argValue = (int) 0;
                } else if (type.equals(List.class)) {
                    argValue = new ArrayList<Object>(0);
                } else if (type.equals(Long.TYPE)) {
                    argValue = (long) 0;
                }
            }

            argValues.add(argValue);
        }
    }

    return (Object[]) argValues.toArray(new Object[argValues.size()]);
}

From source file:com.liferay.wiki.web.internal.display.context.util.WikiSocialActivityHelper.java

License:Open Source License

public String getSocialActivityDescription(WikiPage page, SocialActivity socialActivity,
        JSONObject extraDataJSONObject, ResourceBundle resourceBundle) throws PortalException {

    double version = extraDataJSONObject.getDouble("version", 0);

    WikiPage socialActivityWikiPage = null;

    if (version == 0) {
        socialActivityWikiPage = WikiPageLocalServiceUtil.fetchPage(page.getNodeId(), page.getTitle());
    } else {//from  w w w  . jav a  2 s  . c  o  m
        socialActivityWikiPage = WikiPageLocalServiceUtil.fetchPage(page.getNodeId(), page.getTitle(), version);
    }

    User socialActivityUser = UserLocalServiceUtil.fetchUser(socialActivity.getUserId());

    if (socialActivityUser == null) {
        socialActivityUser = UserLocalServiceUtil.getDefaultUser(socialActivity.getCompanyId());
    }

    String userName = HtmlUtil.escape(socialActivityUser.getFullName());

    if (Validator.isNull(userName)) {
        userName = "Liferay";
    }

    int type = socialActivity.getType();

    if ((type == SocialActivityConstants.TYPE_ADD_ATTACHMENT)
            || (type == SocialActivityConstants.TYPE_MOVE_ATTACHMENT_TO_TRASH)
            || (type == SocialActivityConstants.TYPE_RESTORE_ATTACHMENT_FROM_TRASH)) {

        String label = "x-added-the-attachment-x";

        if (type == SocialActivityConstants.TYPE_MOVE_ATTACHMENT_TO_TRASH) {
            label = "x-removed-the-attachment-x";
        } else if (type == SocialActivityConstants.TYPE_RESTORE_ATTACHMENT_FROM_TRASH) {

            label = "x-restored-the-attachment-x";
        }

        String title = extraDataJSONObject.getString("fileEntryTitle");

        long fileEntryId = extraDataJSONObject.getLong("fileEntryId");

        String url = getDownloadURL(fileEntryId);

        String titleLink = getLink(title, url);

        return LanguageUtil.format(resourceBundle, label, new Object[] { userName, titleLink }, false);
    } else if (type == SocialActivityConstants.TYPE_ADD_COMMENT) {
        LiferayPortletResponse liferayPortletResponse = _wikiRequestHelper.getLiferayPortletResponse();

        StringBundler sb = new StringBundler(4);

        sb.append(getPageURL(page));
        sb.append("#");
        sb.append(liferayPortletResponse.getNamespace());
        sb.append("wikiCommentsPanel");

        return LanguageUtil.format(resourceBundle, "x-added-a-comment",
                new Object[] { userName, sb.toString() }, false);
    } else if ((type == SocialActivityConstants.TYPE_MOVE_TO_TRASH)
            || (type == SocialActivityConstants.TYPE_RESTORE_FROM_TRASH) || (type == WikiActivityKeys.ADD_PAGE)
            || (type == WikiActivityKeys.UPDATE_PAGE)) {

        String pageURL = null;

        if (version == 0) {
            pageURL = getPageURL(socialActivityWikiPage);
        } else {
            pageURL = getPageURL(socialActivityWikiPage, version);
        }

        if (type == SocialActivityConstants.TYPE_MOVE_TO_TRASH) {
            return LanguageUtil.format(resourceBundle, "activity-wiki-page-move-to-trash",
                    new Object[] { StringPool.BLANK, userName, page.getTitle() }, false);
        } else if (type == SocialActivityConstants.TYPE_RESTORE_FROM_TRASH) {
            String titleLink = getLink(page.getTitle(), pageURL);

            return LanguageUtil.format(resourceBundle, "activity-wiki-page-restore-from-trash",
                    new Object[] { StringPool.BLANK, userName, titleLink }, false);
        } else if (type == WikiActivityKeys.ADD_PAGE) {
            String titleLink = getLink(page.getTitle(), pageURL.toString());

            return LanguageUtil.format(resourceBundle, "x-added-the-page-x",
                    new Object[] { userName, titleLink }, false);
        } else if (type == WikiActivityKeys.UPDATE_PAGE) {
            String title = String.valueOf(version);
            String url = pageURL;

            if ((socialActivityWikiPage != null) && socialActivityWikiPage.isMinorEdit()) {

                title += String.format(" (%s)", LanguageUtil.get(resourceBundle, "minor-edit"));
            }

            String titleURL = getLink(title, url);

            return LanguageUtil.format(resourceBundle, "x-updated-the-page-to-version-x",
                    new Object[] { userName, titleURL }, false);
        } else {
            return StringPool.BLANK;
        }
    } else {
        return StringPool.BLANK;
    }
}

From source file:org.sidate.qanda.service.impl.QuestionLocalServiceImpl.java

License:Open Source License

public Question addQuestion(String title, String text, boolean isQuestionToProcedure, long ProcedureId,
        ServiceContext serviceContext) throws PortalException {

    String portletId = serviceContext.getPortletId();
    long groupId = serviceContext.getScopeGroupId();
    long questionId = counterLocalService.increment();
    Date createDate = serviceContext.getCreateDate();
    Date modifiedDate = serviceContext.getModifiedDate();
    String uuid = serviceContext.getUuid();
    Question question = questionPersistence.create(questionId);
    long userId = serviceContext.getUserId();

    question.setUserName(UserLocalServiceUtil.fetchUser(userId).getFullName());
    question.setUuid(uuid);//ww w.jav  a2s  .co m
    question.setCreateDate(createDate);
    question.setModifiedDate(modifiedDate);
    question.setUserId(userId);
    question.setGroupId(groupId);
    question.setExpandoBridgeAttributes(serviceContext);
    question.setIsAnswered(false);
    question.setPortletId(portletId);
    question.setIsQuestionToProcedure(isQuestionToProcedure);
    question.setProcedureId(ProcedureId);

    questionPersistence.update(question);

    resourceLocalService.addModelResources(question, serviceContext);

    assetEntryLocalService.updateEntry(serviceContext.getUserId(), question.getGroupId(),
            question.getCreateDate(), question.getModifiedDate(), Question.class.getName(),
            question.getPrimaryKey(), question.getUuid(), 0, serviceContext.getAssetCategoryIds(),
            serviceContext.getAssetTagNames(), true, true, null, null, null, null, ContentTypes.TEXT_HTML,
            title, text, "", null, null, 0, 0, 0D);

    Indexer<Question> indexer = IndexerRegistryUtil.nullSafeGetIndexer(Question.class);
    indexer.reindex(question);

    socialActivityLocalService.addActivity(userId, groupId, Question.class.getName(), questionId, 1, "", 0);

    log.info("Question " + questionId + " has been added and indexed.");

    return question;
}