Example usage for com.liferay.portal.kernel.service ServiceContext setAttributes

List of usage examples for com.liferay.portal.kernel.service ServiceContext setAttributes

Introduction

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

Prototype

public void setAttributes(Map<String, Serializable> attributes) 

Source Link

Document

Sets the map of the name/value pairs that are the standard parameters of this service context.

Usage

From source file:com.liferay.exportimport.messaging.BasePublisherMessageListener.java

License:Open Source License

protected void initThreadLocals(long userId, Map<String, String[]> parameterMap) throws PortalException {

    User user = UserLocalServiceUtil.getUserById(userId);

    CompanyThreadLocal.setCompanyId(user.getCompanyId());

    PrincipalThreadLocal.setName(userId);

    PermissionChecker permissionChecker = null;

    try {/*w w w .  ja v a  2s  .  co m*/
        permissionChecker = PermissionCheckerFactoryUtil.create(user);
    } catch (Exception e) {
        throw new SystemException("Unable to initialize thread locals because an error occured "
                + "when creating a permission checker for user " + userId, e);
    }

    PermissionThreadLocal.setPermissionChecker(permissionChecker);

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setCompanyId(user.getCompanyId());
    serviceContext.setPathMain(PortalUtil.getPathMain());
    serviceContext.setSignedIn(!user.isDefaultUser());
    serviceContext.setUserId(user.getUserId());

    Map<String, Serializable> attributes = new HashMap<>();

    for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
        String param = entry.getKey();
        String[] values = entry.getValue();

        if (ArrayUtil.isNotEmpty(values)) {
            if (values.length == 1) {
                attributes.put(param, values[0]);
            } else {
                attributes.put(param, values);
            }
        }
    }

    serviceContext.setAttributes(attributes);

    ServiceContextThreadLocal.pushServiceContext(serviceContext);
}