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

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

Introduction

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

Prototype

public void setUserId(long userId) 

Source Link

Document

Sets the ID of this service context's current user.

Usage

From source file:com.bemis.portal.customer.service.impl.CustomerLocationLocalServiceImpl.java

License:Open Source License

@Override
public void updateOrAddCustomerLocation(List<CustomerLocation> customerLocations) throws PortalException {

    for (CustomerLocation customerLocation : customerLocations) {
        long companyId = _bemisPortalService.getDefaultCompanyId();

        User user = _bemisPortalService.getDefaultUser();

        ServiceContext serviceContext = new ServiceContext();

        serviceContext.setCompanyId(companyId);
        serviceContext.setUserId(user.getUserId());

        CustomerLocation storedCustomerLocation = customerLocationPersistence
                .fetchByBemisCustomerId(customerLocation.getBemisCustomerId());

        if (storedCustomerLocation == null) {
            storedCustomerLocation = addCustomerLocation(customerLocation.getBemisCustomerId(),
                    customerLocation.getBemisParentId(), customerLocation.getCustomerName(),
                    customerLocation.getCustomerAddress1(), customerLocation.getCustomerAddress2(),
                    customerLocation.getCustomerAddress3(), customerLocation.getCustomerCity(),
                    customerLocation.getCustomerState(), customerLocation.getCustomerPostalCde(),
                    customerLocation.getCustomerCountry(), customerLocation.getCustomerPhoneNbr(),
                    customerLocation.getCustomerPoBox(), customerLocation.getCustomerPoCity(),
                    customerLocation.getCustomerPoState(), customerLocation.getCustomerPoPostalCde(),
                    customerLocation.getCustomerActiveStatusCde(), serviceContext);
        } else {/*from  w  w w.  j a  va  2 s  .c o m*/
            storedCustomerLocation = updateCustomerLocation(storedCustomerLocation.getCustomerLocationId(),
                    customerLocation.getBemisParentId(), customerLocation.getCustomerName(),
                    customerLocation.getCustomerAddress1(), customerLocation.getCustomerAddress2(),
                    customerLocation.getCustomerAddress3(), customerLocation.getCustomerCity(),
                    customerLocation.getCustomerState(), customerLocation.getCustomerPostalCde(),
                    customerLocation.getCustomerCountry(), customerLocation.getCustomerPhoneNbr(),
                    customerLocation.getCustomerPoBox(), customerLocation.getCustomerPoCity(),
                    customerLocation.getCustomerPoState(), customerLocation.getCustomerPoPostalCde(),
                    customerLocation.getCustomerActiveStatusCde(), serviceContext);
        }
    }
}

From source file:com.bemis.portal.fileuploader.service.impl.FileUploaderLocalServiceImpl.java

License:Open Source License

/**
 * Uploads the file into the destination folder
 * If the file does not exist, adds the file. If exists, updates the existing file
 * Adds tags and categories to documents
 *
 * @param companyId/*from  w w  w  .j a v a 2  s . com*/
 * @param groupId
 * @param userId
 * @param file
 * @param fileDescription
 * @param title
 * @param folderId
 * @param changeLog
 * @param assetTagNames
 * @return FileEntry
 * @throws PortalException
 */
public FileEntry uploadFile(long companyId, long groupId, long userId, File file, String fileDescription,
        String title, long folderId, String changeLog, String[] assetTagNames) throws PortalException {

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setCompanyId(companyId);
    serviceContext.setScopeGroupId(groupId);
    serviceContext.setUserId(userId);
    serviceContext.setAssetTagNames(assetTagNames);
    serviceContext.setAttribute(FileUploaderConstants.DESTINATION_FOLDER_ID, folderId);

    return uploadFile(file, fileDescription, title, changeLog, serviceContext);
}

From source file:com.liferay.asset.test.util.BaseAssetSearchTestCase.java

License:Open Source License

protected List<BaseModel<?>> addBaseModels(Group[] groups, String keywords, ServiceContext serviceContext)
        throws Exception {

    List<BaseModel<?>> baseModels = new ArrayList<>();

    for (Group group : groups) {
        User user = UserTestUtil.getAdminUser(group.getCompanyId());

        serviceContext.setCompanyId(group.getCompanyId());
        serviceContext.setScopeGroupId(group.getGroupId());
        serviceContext.setUserId(user.getUserId());

        BaseModel<?> parentBaseModel = getParentBaseModel(group, serviceContext);

        baseModels.add(addBaseModel(parentBaseModel, keywords, serviceContext));
    }//from  w ww.  j a  v a2 s  .c  om

    return baseModels;
}

From source file:com.liferay.calendar.service.test.CalendarBookingServiceTest.java

License:Open Source License

protected ServiceContext createServiceContext() {
    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setCompanyId(_user1.getCompanyId());
    serviceContext.setUserId(_user1.getUserId());

    return serviceContext;
}

From source file:com.liferay.calendar.test.util.CalendarNotificationTemplateTestUtil.java

License:Open Source License

protected static ServiceContext createServiceContext(User user) {
    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setCompanyId(user.getCompanyId());

    serviceContext.setUserId(user.getUserId());

    return serviceContext;
}

From source file:com.liferay.calendar.test.util.CalendarStagingTestUtil.java

License:Open Source License

public static ServiceContext createServiceContext(User user) {
    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setCompanyId(user.getCompanyId());
    serviceContext.setUserId(user.getUserId());

    return serviceContext;
}

From source file:com.liferay.document.library.internal.exportimport.data.handler.FileEntryStagedModelDataHandler.java

License:Open Source License

@Override
protected void doImportStagedModel(PortletDataContext portletDataContext, FileEntry fileEntry)
        throws Exception {

    long userId = portletDataContext.getUserId(fileEntry.getUserUuid());

    if (!fileEntry.isDefaultRepository()) {

        // References has been automatically imported, nothing to do here

        return;/*from ww  w .j  a va  2s .c  om*/
    }

    Map<Long, Long> folderIds = (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(Folder.class);

    long folderId = MapUtil.getLong(folderIds, fileEntry.getFolderId(), fileEntry.getFolderId());

    long[] assetCategoryIds = portletDataContext.getAssetCategoryIds(DLFileEntry.class,
            fileEntry.getFileEntryId());
    String[] assetTagNames = portletDataContext.getAssetTagNames(DLFileEntry.class, fileEntry.getFileEntryId());

    ServiceContext serviceContext = portletDataContext.createServiceContext(fileEntry, DLFileEntry.class);

    serviceContext.setAttribute("sourceFileName", "A." + fileEntry.getExtension());
    serviceContext.setUserId(userId);

    Element fileEntryElement = portletDataContext.getImportDataElement(fileEntry);

    String binPath = fileEntryElement.attributeValue("bin-path");

    InputStream is = null;

    try {
        if (Validator.isNull(binPath) && portletDataContext.isPerformDirectBinaryImport()) {

            try {
                is = FileEntryUtil.getContentStream(fileEntry);
            } catch (Exception e) {
                if (_log.isWarnEnabled()) {
                    _log.warn("Unable to retrieve content for file entry " + fileEntry.getFileEntryId(), e);
                }

                return;
            }
        } else {
            is = portletDataContext.getZipEntryAsInputStream(binPath);
        }

        if (is == null) {
            if (_log.isWarnEnabled()) {
                _log.warn("No file found for file entry " + fileEntry.getFileEntryId());
            }

            return;
        }

        importMetaData(portletDataContext, fileEntryElement, fileEntry, serviceContext);

        FileEntry importedFileEntry = null;

        if (portletDataContext.isDataStrategyMirror()) {
            FileEntry existingFileEntry = fetchStagedModelByUuidAndGroupId(fileEntry.getUuid(),
                    portletDataContext.getScopeGroupId());

            FileVersion fileVersion = fileEntry.getFileVersion();

            if (existingFileEntry == null) {
                if (portletDataContext.isDataStrategyMirrorWithOverwriting()) {

                    FileEntry existingTitleFileEntry = FileEntryUtil
                            .fetchByR_F_T(portletDataContext.getScopeGroupId(), folderId, fileEntry.getTitle());

                    if (existingTitleFileEntry == null) {
                        existingTitleFileEntry = FileEntryUtil.fetchByR_F_FN(
                                portletDataContext.getScopeGroupId(), folderId, fileEntry.getFileName());
                    }

                    if (existingTitleFileEntry != null) {
                        _dlAppLocalService.deleteFileEntry(existingTitleFileEntry.getFileEntryId());
                    }
                }

                serviceContext.setAttribute("fileVersionUuid", fileVersion.getUuid());
                serviceContext.setUuid(fileEntry.getUuid());

                String fileEntryTitle = _dlFileEntryLocalService.getUniqueTitle(
                        portletDataContext.getScopeGroupId(), folderId, 0, fileEntry.getTitle(),
                        fileEntry.getExtension());

                importedFileEntry = _dlAppLocalService.addFileEntry(userId,
                        portletDataContext.getScopeGroupId(), folderId, fileEntry.getFileName(),
                        fileEntry.getMimeType(), fileEntryTitle, fileEntry.getDescription(), null, is,
                        fileEntry.getSize(), serviceContext);

                if (fileEntry.isInTrash()) {
                    importedFileEntry = _dlTrashService
                            .moveFileEntryToTrash(importedFileEntry.getFileEntryId());
                }
            } else {
                FileVersion latestExistingFileVersion = existingFileEntry.getLatestFileVersion(true);

                boolean indexEnabled = serviceContext.isIndexingEnabled();

                boolean deleteFileEntry = false;
                boolean updateFileEntry = false;

                if (!Objects.equals(fileVersion.getUuid(), latestExistingFileVersion.getUuid())) {

                    deleteFileEntry = true;
                    updateFileEntry = true;
                } else {
                    InputStream existingFileVersionInputStream = null;

                    try {
                        existingFileVersionInputStream = latestExistingFileVersion.getContentStream(false);
                    } catch (Exception e) {
                        if (_log.isDebugEnabled()) {
                            _log.debug(e, e);
                        }
                    } finally {
                        if (existingFileVersionInputStream != null) {
                            existingFileVersionInputStream.close();
                        }
                    }

                    if (existingFileVersionInputStream == null) {
                        updateFileEntry = true;
                    }
                }

                try {
                    serviceContext.setIndexingEnabled(false);

                    if (updateFileEntry) {
                        DLFileVersion alreadyExistingFileVersion = _dlFileVersionLocalService
                                .getFileVersionByUuidAndGroupId(fileVersion.getUuid(),
                                        existingFileEntry.getGroupId());

                        if (alreadyExistingFileVersion != null) {
                            serviceContext.setAttribute("existingDLFileVersionId",
                                    alreadyExistingFileVersion.getFileVersionId());
                        }

                        serviceContext.setUuid(fileVersion.getUuid());

                        String fileEntryTitle = _dlFileEntryLocalService.getUniqueTitle(
                                portletDataContext.getScopeGroupId(), existingFileEntry.getFolderId(),
                                existingFileEntry.getFileEntryId(), fileEntry.getTitle(),
                                fileEntry.getExtension());

                        importedFileEntry = _dlAppLocalService.updateFileEntry(userId,
                                existingFileEntry.getFileEntryId(), fileEntry.getFileName(),
                                fileEntry.getMimeType(), fileEntryTitle, fileEntry.getDescription(), null,
                                false, is, fileEntry.getSize(), serviceContext);
                    } else {
                        _dlAppLocalService.updateAsset(userId, existingFileEntry, latestExistingFileVersion,
                                assetCategoryIds, assetTagNames, null);

                        importedFileEntry = existingFileEntry;
                    }

                    if (importedFileEntry.getFolderId() != folderId) {
                        importedFileEntry = _dlAppLocalService.moveFileEntry(userId,
                                importedFileEntry.getFileEntryId(), folderId, serviceContext);
                    }

                    if (importedFileEntry instanceof LiferayFileEntry) {
                        LiferayFileEntry liferayFileEntry = (LiferayFileEntry) importedFileEntry;

                        Indexer<DLFileEntry> indexer = IndexerRegistryUtil
                                .nullSafeGetIndexer(DLFileEntry.class);

                        indexer.reindex((DLFileEntry) liferayFileEntry.getModel());
                    }

                    if (deleteFileEntry && ExportImportThreadLocal.isStagingInProcess()) {

                        _dlAppService.deleteFileVersion(latestExistingFileVersion.getFileEntryId(),
                                latestExistingFileVersion.getVersion());
                    }
                } finally {
                    serviceContext.setIndexingEnabled(indexEnabled);
                }
            }
        } else {
            String fileEntryTitle = _dlFileEntryLocalService.getUniqueTitle(
                    portletDataContext.getScopeGroupId(), folderId, 0, fileEntry.getTitle(),
                    fileEntry.getExtension());

            importedFileEntry = _dlAppLocalService.addFileEntry(userId, portletDataContext.getScopeGroupId(),
                    folderId, fileEntry.getFileName(), fileEntry.getMimeType(), fileEntryTitle,
                    fileEntry.getDescription(), null, is, fileEntry.getSize(), serviceContext);
        }

        for (DLPluggableContentDataHandler dlPluggableContentDataHandler : _serviceTrackerList) {

            dlPluggableContentDataHandler.importContent(portletDataContext, fileEntryElement, fileEntry,
                    importedFileEntry);
        }

        portletDataContext.importClassedModel(fileEntry, importedFileEntry, DLFileEntry.class);

        Map<Long, Long> fileEntryIds = (Map<Long, Long>) portletDataContext
                .getNewPrimaryKeysMap(FileEntry.class);

        fileEntryIds.put(fileEntry.getFileEntryId(), importedFileEntry.getFileEntryId());
    } finally {
        try {
            is.close();
        } catch (IOException ioe) {
            _log.error(ioe, ioe);
        }
    }
}

From source file:com.liferay.document.library.internal.exportimport.data.handler.FolderStagedModelDataHandler.java

License:Open Source License

@Override
protected void doImportStagedModel(PortletDataContext portletDataContext, Folder folder) throws Exception {

    Map<Long, Long> folderIdsAndRepositoryEntryIds = (Map<Long, Long>) portletDataContext
            .getNewPrimaryKeysMap(Folder.class + ".folderIdsAndRepositoryEntryIds");

    if (!folder.isDefaultRepository()) {
        Map<Long, Long> repositoryEntryIds = (Map<Long, Long>) portletDataContext
                .getNewPrimaryKeysMap(RepositoryEntry.class);

        folderIdsAndRepositoryEntryIds.put(folder.getFolderId(), repositoryEntryIds.get(folder.getFolderId()));

        return;//from w w w . ja v  a 2  s .c  o m
    }

    long userId = portletDataContext.getUserId(folder.getUserUuid());

    Map<Long, Long> folderIds = (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(Folder.class);

    long parentFolderId = MapUtil.getLong(folderIds, folder.getParentFolderId(), folder.getParentFolderId());

    ServiceContext serviceContext = portletDataContext.createServiceContext(folder, DLFolder.class);

    serviceContext.setUserId(userId);

    Folder importedFolder = null;

    if (portletDataContext.isDataStrategyMirror()) {
        Folder existingFolder = fetchStagedModelByUuidAndGroupId(folder.getUuid(),
                portletDataContext.getScopeGroupId());

        if (existingFolder == null) {
            String name = getFolderName(null, portletDataContext.getScopeGroupId(), parentFolderId,
                    folder.getName(), 2);

            serviceContext.setUuid(folder.getUuid());

            importedFolder = _dlAppLocalService.addFolder(userId, portletDataContext.getScopeGroupId(),
                    parentFolderId, name, folder.getDescription(), serviceContext);
        } else {
            String name = getFolderName(folder.getUuid(), portletDataContext.getScopeGroupId(), parentFolderId,
                    folder.getName(), 2);

            importedFolder = _dlAppLocalService.updateFolder(existingFolder.getFolderId(), parentFolderId, name,
                    folder.getDescription(), serviceContext);
        }
    } else {
        String name = getFolderName(null, portletDataContext.getScopeGroupId(), parentFolderId,
                folder.getName(), 2);

        importedFolder = _dlAppLocalService.addFolder(userId, portletDataContext.getScopeGroupId(),
                parentFolderId, name, folder.getDescription(), serviceContext);
    }

    Element folderElement = portletDataContext.getImportDataElement(folder);

    importFolderFileEntryTypes(portletDataContext, folderElement, folder, importedFolder, serviceContext);

    portletDataContext.importClassedModel(folder, importedFolder, DLFolder.class);

    folderIds.put(folder.getFolderId(), importedFolder.getFolderId());
    folderIdsAndRepositoryEntryIds.put(folder.getFolderId(), importedFolder.getFolderId());
}

From source file:com.liferay.document.library.internal.instance.lifecycle.AddDefaultDocumentLibraryStructuresPortalInstanceLifecycleListener.java

License:Open Source License

@Override
public void portalInstanceRegistered(Company company) throws Exception {
    if (!_dlConfiguration.addDefaultStructures()) {
        return;//w  w w .ja v a  2 s  .  c om
    }

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setAddGuestPermissions(true);
    serviceContext.setAddGroupPermissions(true);

    Group group = _groupLocalService.getCompanyGroup(company.getCompanyId());

    serviceContext.setScopeGroupId(group.getGroupId());

    long defaultUserId = _userLocalService.getDefaultUserId(company.getCompanyId());

    serviceContext.setUserId(defaultUserId);

    _defaultDDMStructureHelper.addDDMStructures(defaultUserId, group.getGroupId(),
            _portal.getClassNameId(DLFileEntryMetadata.class), getClassLoader(),
            "com/liferay/document/library/events/dependencies" + "/document-library-structures.xml",
            serviceContext);

    addDLFileEntryTypes(defaultUserId, group.getGroupId(), serviceContext);

    addDLRawMetadataStructures(defaultUserId, group.getGroupId(), serviceContext);
}

From source file:com.liferay.document.library.web.internal.webdav.DLWebDAVStorageImpl.java

License:Open Source License

@Override
public int moveCollectionResource(WebDAVRequest webDAVRequest, Resource resource, String destination,
        boolean overwrite) throws WebDAVException {

    try {/*from   ww  w . j a  va  2s .co m*/
        String[] destinationArray = WebDAVUtil.getPathArray(destination, true);

        Folder folder = (Folder) resource.getModel();

        long companyId = webDAVRequest.getCompanyId();

        long groupId = WebDAVUtil.getGroupId(companyId, destinationArray);

        long folderId = folder.getFolderId();
        long parentFolderId = getParentFolderId(companyId, destinationArray);
        String name = WebDAVUtil.getResourceName(destinationArray);
        String description = folder.getDescription();

        ServiceContext serviceContext = new ServiceContext();

        serviceContext.setUserId(webDAVRequest.getUserId());

        int status = HttpServletResponse.SC_CREATED;

        if (overwrite) {
            if (deleteResource(groupId, parentFolderId, name, webDAVRequest.getLockUuid())) {

                status = HttpServletResponse.SC_NO_CONTENT;
            }
        }

        if (parentFolderId != folder.getParentFolderId()) {
            _dlAppService.moveFolder(folderId, parentFolderId, serviceContext);
        }

        if (!name.equals(folder.getName())) {
            _dlAppService.updateFolder(folderId, name, description, serviceContext);
        }

        return status;
    } catch (PrincipalException pe) {
        if (_log.isDebugEnabled()) {
            _log.debug(pe, pe);
        }

        return HttpServletResponse.SC_FORBIDDEN;
    } catch (DuplicateFolderNameException dfne) {
        if (_log.isDebugEnabled()) {
            _log.debug(dfne, dfne);
        }

        return HttpServletResponse.SC_PRECONDITION_FAILED;
    } catch (Exception e) {
        throw new WebDAVException(e);
    }
}