Cleans up empty workflow projects.<p>

@param projects the workflow projects to clean up

@throws CmsException if something goes wrong
546074
protected void cleanupEmptyWorkflowProjects(List<CmsProject> projects) throws CmsException {

        if (projects == null) {
            projects = OpenCms.getOrgUnitManager().getAllManageableProjects(m_adminCms, "", true);
        }
        for (CmsProject project : projects) {
            if (project.isWorkflowProject()) {
                if (isProjectEmpty(project)) {
                    m_adminCms.deleteProject(project.getUuid());
                }
            }
        }
    }
1092147
private void deleteLeastRecentlyUsedProjects(long sizeToFreeInBytes,
      final List<ProjectDirectoryMetadata> projectDirMetadataList) {
    // Sort projects by last reference time in ascending order
    projectDirMetadataList.sort(Comparator.comparing(ProjectDirectoryMetadata::getLastAccessTime));
    final Set<File> projectDirsToDelete = new HashSet<>();

    for (final ProjectDirectoryMetadata proj : projectDirMetadataList) {
      if (sizeToFreeInBytes > 0) {
        // Delete the project directory even if flow within is running. It's OK to
        // delete the directory since execution dir is HARD linked to project dir. Note that even
        // if project is deleted, disk space will be freed up only when all associated execution
        // dirs are deleted.
        if (proj.getInstalledDir() != null) {
          projectDirsToDelete.add(proj.getInstalledDir());
          sizeToFreeInBytes -= proj.getDirSizeInByte();
        }
      } else {
        break;
      }
    }

    final long start = System.currentTimeMillis();
    deleteProjectDirsInParallel(ImmutableSet.copyOf(projectDirsToDelete));
    final long end = System.currentTimeMillis();
    log.info("Deleting {} project dir(s) took {} sec(s)", projectDirsToDelete.size(),
        (end - start) / 1000);
  }
942708
protected void scrubResource(
        CmsObject cms,
        CmsPublishedResource res,
        Set<String> scrubbedFolders,
        Set<String> scrubbedFiles) {

        long timer = System.currentTimeMillis();
        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_SCRUB_EXPORT_START_RESOURCE_1, res.getRootPath()));
        }
        try {
            // ensure all siblings are scrubbed if the resource has one
            String resPath = cms.getRequestContext().removeSiteRoot(res.getRootPath());
            List<String> siblings = getSiblingsList(cms, resPath);

            for (String vfsName : siblings) {

                // get the link name for the published file
                String rfsName = OpenCms.getStaticExportManager().getRfsName(cms, vfsName);
                if (LOG.isDebugEnabled()) {
                    LOG.debug(Messages.get().getBundle().key(Messages.LOG_CHECKING_STATIC_EXPORT_2, vfsName, rfsName));
                }
                if (rfsName.startsWith(OpenCms.getStaticExportManager().getRfsPrefix(vfsName))
                    && (!scrubbedFiles.contains(rfsName))
                    && (!scrubbedFolders.contains(CmsResource.getFolderPath(rfsName)))) {

                    if (res.isFolder()) {
                        if (res.getState().isDeleted()) {
                            String exportFolderName = CmsFileUtil.normalizePath(
                                OpenCms.getStaticExportManager().getExportPath(vfsName)
                                    + rfsName.substring(
                                        OpenCms.getStaticExportManager().getRfsPrefix(vfsName).length()));
                            try {
                                File exportFolder = new File(exportFolderName);
                                // check if export folder exists, if so delete it
                                if (exportFolder.exists() && exportFolder.canWrite()) {
                                    CmsFileUtil.purgeDirectory(exportFolder);
                                    // write log message
                                    if (LOG.isInfoEnabled()) {
                                        LOG.info(
                                            Messages.get().getBundle().key(
                                                Messages.LOG_FOLDER_DELETED_1,
                                                exportFolderName));
                                    }
                                    scrubbedFolders.add(rfsName);
                                    continue;
                                }
                            } catch (Throwable t) {
                                // ignore, nothing to do about this
                                if (LOG.isWarnEnabled()) {
                                    LOG.warn(
                                        Messages.get().getBundle().key(
                                            Messages.LOG_FOLDER_DELETION_FAILED_2,
                                            vfsName,
                                            exportFolderName));
                                }
                            }
                        }
                    } else {
                        // check if the file is the default file of the folder
                        try {
                            CmsResource defaultFile = cms.readDefaultFile(CmsResource.getFolderPath(vfsName));
                            if (defaultFile != null) {
                                String defaultfilePath = cms.getRequestContext().removeSiteRoot(
                                    defaultFile.getRootPath());
                                if (vfsName.equals(defaultfilePath)) {
                                    // this is the default file, remove it additionally if present
                                    String rfsNameDefault = CmsResource.getFolderPath(rfsName)
                                        + CmsStaticExportManager.EXPORT_DEFAULT_FILE;
                                    String rfsExportFileName = CmsFileUtil.normalizePath(
                                        OpenCms.getStaticExportManager().getExportPath(vfsName)
                                            + rfsNameDefault.substring(
                                                OpenCms.getStaticExportManager().getRfsPrefix(vfsName).length()));

                                    purgeFile(rfsExportFileName, vfsName);
                                }
                            }
                        } catch (CmsException e) {
                            // failed to determine default file
                        }
                    }

                    // add index_export.html or the index.html to the folder name
                    rfsName = OpenCms.getStaticExportManager().addDefaultFileNameToFolder(rfsName, res.isFolder());
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(Messages.get().getBundle().key(Messages.LOG_RFSNAME_1, rfsName));
                    }
                    String rfsExportFileName = CmsFileUtil.normalizePath(
                        OpenCms.getStaticExportManager().getExportPath(vfsName)
                            + rfsName.substring(OpenCms.getStaticExportManager().getRfsPrefix(vfsName).length()));
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(Messages.get().getBundle().key(Messages.LOG_EXPORT_RFSNAME_1, rfsName));
                    }
                    // purge related files
                    List<File> relFilesToPurge = getRelatedFilesToPurge(rfsExportFileName, vfsName);
                    purgeFiles(relFilesToPurge, vfsName, scrubbedFiles);

                    if (!res.isFolder()) {
                        I_CmsResourceType resType = OpenCms.getResourceManager().getResourceType(res.getType());
                        I_CmsResourceLoader resLoader = OpenCms.getResourceManager().getLoader(resType.getLoaderId());
                        if ((resLoader instanceof CmsXmlContentLoader)
                            && !(resLoader instanceof CmsXmlContainerPageLoader)) {

                            // only execute for XML content that are no container pages
                            List<File> detailPageFiles = getDetailPageFiles(cms, res, vfsName);
                            purgeFiles(detailPageFiles, vfsName, scrubbedFiles);
                            if (LOG.isDebugEnabled()) {
                                LOG.debug(Messages.get().getBundle().key(Messages.LOG_PURGED_DETAILPAGES_0));
                            }
                            List<File> referencingContainerPages = getContainerPagesToPurge(cms, res.getStructureId());
                            purgeFiles(referencingContainerPages, vfsName, scrubbedFiles);
                            if (LOG.isDebugEnabled()) {
                                LOG.debug(Messages.get().getBundle().key(Messages.LOG_PURGED_CONTAINERPAGES_0));
                            }
                        }
                    }
                    // purge the file itself
                    purgeFile(rfsExportFileName, vfsName);
                    scrubbedFiles.add(rfsName);
                }
            }
        } catch (Throwable e) {
            LOG.error(e.getLocalizedMessage(), e);
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug(
                Messages.get().getBundle().key(
                    Messages.LOG_SCRUB_EXPORT_FINISH_RESOURCE_2,
                    res.getRootPath(),
                    (System.currentTimeMillis() - timer) + ""));
        }
    }
274628
private void setLastWorkflowForUser(String workflowId) throws CmsException {

        CmsUser user = getCmsObject().getRequestContext().getCurrentUser();
        user.setAdditionalInfo(PARAM_WORKFLOW_ID, workflowId);
        getCmsObject().writeUser(user);
    }
248542
protected void deleteRelationsWithSiblings(CmsDbContext dbc, CmsResource resource) throws CmsException {

        // get all siblings
        List<CmsResource> siblings;
        if (resource.getSiblingCount() > 1) {
            siblings = readSiblings(dbc, resource, CmsResourceFilter.ALL);
        } else {
            siblings = new ArrayList<CmsResource>();
            siblings.add(resource);
        }
        // clean the relations in content for all siblings
        I_CmsVfsDriver vfsDriver = getVfsDriver(dbc);
        Iterator<CmsResource> it = siblings.iterator();
        while (it.hasNext()) {
            CmsResource sibling = it.next();
            // clean the relation information for this sibling
            vfsDriver.deleteRelations(
                dbc,
                dbc.currentProject().getUuid(),
                sibling,
                CmsRelationFilter.TARGETS.filterDefinedInContent());
        }
    }
196857
public void removeResourcesInProject(CmsUUID projectId, boolean removeSystemLocks) {

        Iterator<CmsLock> itLocks = OpenCms.getMemoryMonitor().getAllCachedLocks().iterator();
        while (itLocks.hasNext()) {
            CmsLock currentLock = itLocks.next();
            if (removeSystemLocks && currentLock.getSystemLock().getProjectId().equals(projectId)) {
                unlockResource(currentLock.getResourceName(), true);
            }
            if (currentLock.getEditionLock().getProjectId().equals(projectId)) {
                unlockResource(currentLock.getResourceName(), false);
            }
        }
    }
1022741
private boolean performDefineOperation() throws CmsException {

        boolean useTempfileProject = Boolean.valueOf(getParamUsetempfileproject()).booleanValue();
        try {
            if (useTempfileProject) {
                switchToTempProject();
            }
            String newProperty = getParamNewproperty();
            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(newProperty)) {
                getCms().createPropertyDefinition(newProperty);
                return true;
            } else {
                throw new CmsException(Messages.get().container(Messages.ERR_INVALID_PROP_0));
            }
        } finally {
            if (useTempfileProject) {
                switchToCurrentProject();
            }
        }
    }
1348695
public static List<CmsProject> getAvailableProjects(CmsObject cms) {

        // get all project information
        List<CmsProject> allProjects;
        try {
            String ouFqn = "";
            CmsUserSettings settings = new CmsUserSettings(cms);
            if (!settings.getListAllProjects()) {
                ouFqn = cms.getRequestContext().getCurrentUser().getOuFqn();
            }
            allProjects = new ArrayList<CmsProject>(
                OpenCms.getOrgUnitManager().getAllAccessibleProjects(cms, ouFqn, settings.getListAllProjects()));
            Iterator<CmsProject> itProjects = allProjects.iterator();
            while (itProjects.hasNext()) {
                CmsProject prj = itProjects.next();
                if (prj.isHiddenFromSelector()) {
                    itProjects.remove();
                }
            }
        } catch (CmsException e) {
            // should usually never happen
            LOG.error(e.getLocalizedMessage(), e);
            allProjects = Collections.emptyList();
        }
        return allProjects;
    }
1144941
private void deleteResourceFromProject(CmsObject cms, CmsResource res) throws CmsException {

        CmsLock lock = cms.getLock(res);
        if (lock.isUnlocked() || lock.isLockableBy(cms.getRequestContext().getCurrentUser())) {
            cms.lockResourceTemporary(res);
        } else {
            cms.changeLock(res);
        }
        cms.deleteResource(cms.getSitePath(res), CmsResource.DELETE_PRESERVE_SIBLINGS);
    }
70904
public void setProjectsNotFound(java.util.Collection<String> projectsNotFound) {
        if (projectsNotFound == null) {
            this.projectsNotFound = null;
            return;
        }

        this.projectsNotFound = new java.util.ArrayList<String>(projectsNotFound);
    }
1136529
public void deleteLogEntries(CmsDbContext dbc, CmsLogFilter filter) throws CmsException {

        updateLog(dbc);
        m_projectDriver.deleteLog(dbc, filter);
    }
762409
public void repairCategories(CmsDbContext dbc, CmsUUID projectId, CmsResource resource) throws CmsException {

        CmsObject cms = OpenCms.initCmsObject(new CmsObject(getSecurityManager(), dbc.getRequestContext()));
        cms.getRequestContext().setSiteRoot("");
        cms.getRequestContext().setCurrentProject(readProject(dbc, projectId));
        CmsCategoryService.getInstance().repairRelations(cms, resource);
    }
1078464
public void setProjects(java.util.Collection<String> projects) {
        if (projects == null) {
            this.projects = null;
            return;
        }

        this.projects = new java.util.ArrayList<String>(projects);
    }
728875
public void deleteAllStaticExportPublishedResources(CmsDbContext dbc, int linkType) throws CmsException {

        getProjectDriver(dbc).deleteAllStaticExportPublishedResources(dbc, linkType);
    }
55156
public void unlockProject(CmsProject project) {

        // unlock all resources in the project
        m_lockManager.removeResourcesInProject(project.getUuid(), false);
        m_monitor.clearResourceCache();
        m_monitor.flushCache(CmsMemoryMonitor.CacheType.PROJECT, CmsMemoryMonitor.CacheType.PERMISSION);
    }
1153044
public void updateSessionInfos(CmsObject cms) {

        // get all sessions
        List<CmsSessionInfo> userSessions = getSessionInfos();
        Iterator<CmsSessionInfo> i = userSessions.iterator();
        while (i.hasNext()) {
            CmsSessionInfo sessionInfo = i.next();
            // check is the project stored in this session is not existing anymore
            // if so, set it to the online project
            CmsUUID projectId = sessionInfo.getProject();
            try {
                cms.readProject(projectId);
            } catch (CmsException e) {
                // the project does not longer exist, update the project information with the online project
                sessionInfo.setProject(CmsProject.ONLINE_PROJECT_ID);
                addSessionInfo(sessionInfo);
            }
        }
    }
1121072
private void lockFlowsForProject(Project project, List<String> lockedFlows) {
    for (String flowId: lockedFlows) {
      Flow flow = project.getFlow(flowId);
      if (flow != null) {
        flow.setLocked(true);
      }
    }
  }