Search in sources :

Example 1 with I_CmsHistoryResource

use of org.opencms.file.history.I_CmsHistoryResource in project opencms-core by alkacon.

the class CmsResourceComparison method compareProperties.

/**
 * Helper method that finds out, which of the properties were added, removed, modified or remain unchanged.<p>
 *
 * @param cms the CmsObject to use
 * @param resource1 the first resource to read the properties from
 * @param version1 the version of the first resource
 * @param resource2 the second resource to read the properties from
 * @param version2 the version of the second resource
 *
 * @return a list of the compared attributes
 *
 * @throws CmsException if something goes wrong
 */
public static List<CmsAttributeComparison> compareProperties(CmsObject cms, CmsResource resource1, String version1, CmsResource resource2, String version2) throws CmsException {
    List<CmsProperty> properties1;
    if (resource1 instanceof I_CmsHistoryResource) {
        properties1 = cms.readHistoryPropertyObjects((I_CmsHistoryResource) resource1);
    } else {
        if (Integer.parseInt(version1) < 0) {
            // switch to the online project
            CmsProject prj = cms.getRequestContext().getCurrentProject();
            try {
                cms.getRequestContext().setCurrentProject(cms.readProject(CmsProject.ONLINE_PROJECT_ID));
                properties1 = cms.readPropertyObjects(resource1, false);
            } finally {
                cms.getRequestContext().setCurrentProject(prj);
            }
        } else {
            properties1 = cms.readPropertyObjects(resource1, false);
        }
    }
    List<CmsProperty> properties2;
    if (resource2 instanceof I_CmsHistoryResource) {
        properties2 = cms.readHistoryPropertyObjects((I_CmsHistoryResource) resource2);
    } else {
        if (Integer.parseInt(version2) < 0) {
            // switch to the online project
            CmsProject prj = cms.getRequestContext().getCurrentProject();
            try {
                cms.getRequestContext().setCurrentProject(cms.readProject(CmsProject.ONLINE_PROJECT_ID));
                properties2 = cms.readPropertyObjects(resource2, false);
            } finally {
                cms.getRequestContext().setCurrentProject(prj);
            }
        } else {
            properties2 = cms.readPropertyObjects(resource2, false);
        }
    }
    List<CmsAttributeComparison> comparedProperties = new ArrayList<CmsAttributeComparison>();
    List<CmsProperty> removedProperties = new ArrayList<CmsProperty>(properties1);
    removedProperties.removeAll(properties2);
    List<CmsProperty> addedProperties = new ArrayList<CmsProperty>(properties2);
    addedProperties.removeAll(properties1);
    List<CmsProperty> retainedProperties = new ArrayList<CmsProperty>(properties2);
    retainedProperties.retainAll(properties1);
    CmsProperty prop;
    Iterator<CmsProperty> i = addedProperties.iterator();
    while (i.hasNext()) {
        prop = i.next();
        comparedProperties.add(new CmsAttributeComparison(prop.getName(), "", prop.getValue(), CmsResourceComparison.TYPE_ADDED));
    }
    i = removedProperties.iterator();
    while (i.hasNext()) {
        prop = i.next();
        comparedProperties.add(new CmsAttributeComparison(prop.getName(), prop.getValue(), "", CmsResourceComparison.TYPE_REMOVED));
    }
    i = retainedProperties.iterator();
    while (i.hasNext()) {
        prop = i.next();
        String value1 = properties1.get(properties1.indexOf(prop)).getValue();
        String value2 = properties2.get(properties2.indexOf(prop)).getValue();
        if (value1.equals(value2)) {
            comparedProperties.add(new CmsAttributeComparison(prop.getName(), value1, value2, CmsResourceComparison.TYPE_UNCHANGED));
        } else {
            comparedProperties.add(new CmsAttributeComparison(prop.getName(), value1, value2, CmsResourceComparison.TYPE_CHANGED));
        }
    }
    return comparedProperties;
}
Also used : I_CmsHistoryResource(org.opencms.file.history.I_CmsHistoryResource) CmsProperty(org.opencms.file.CmsProperty) ArrayList(java.util.ArrayList) CmsProject(org.opencms.file.CmsProject)

Example 2 with I_CmsHistoryResource

use of org.opencms.file.history.I_CmsHistoryResource in project opencms-core by alkacon.

the class CmsDriverManager method restoreDeletedResource.

/**
 * Restores a deleted resource identified by its structure id from the historical archive.<p>
 *
 * @param dbc the current database context
 * @param structureId the structure id of the resource to restore
 *
 * @throws CmsException if something goes wrong
 *
 * @see CmsObject#restoreDeletedResource(CmsUUID)
 */
public void restoreDeletedResource(CmsDbContext dbc, CmsUUID structureId) throws CmsException {
    // get the last version, which should be the deleted one
    int version = getHistoryDriver(dbc).readLastVersion(dbc, structureId);
    // get that version
    I_CmsHistoryResource histRes = getHistoryDriver(dbc).readResource(dbc, structureId, version);
    // check the parent path
    CmsResource parent;
    try {
        // try to read the parent resource by id
        parent = getVfsDriver(dbc).readResource(dbc, dbc.currentProject().getUuid(), histRes.getParentId(), true);
    } catch (CmsVfsResourceNotFoundException e) {
        // if not found try to read the parent resource by name
        try {
            // try to read the parent resource by id
            parent = getVfsDriver(dbc).readResource(dbc, dbc.currentProject().getUuid(), CmsResource.getParentFolder(histRes.getRootPath()), true);
        } catch (CmsVfsResourceNotFoundException e1) {
            // if not found try to restore the parent resource
            restoreDeletedResource(dbc, histRes.getParentId());
            parent = readResource(dbc, histRes.getParentId(), CmsResourceFilter.IGNORE_EXPIRATION);
        }
    }
    // check write permissions
    m_securityManager.checkPermissions(dbc, parent, CmsPermissionSet.ACCESS_WRITE, false, CmsResourceFilter.IGNORE_EXPIRATION);
    // check the name
    String path = parent.getRootPath();
    // name
    String resName = CmsResource.getName(histRes.getRootPath());
    String ext = "";
    if (resName.charAt(resName.length() - 1) == '/') {
        resName = resName.substring(0, resName.length() - 1);
    } else {
        // extension
        ext = CmsFileUtil.getExtension(resName);
    }
    // name without extension
    String nameWOExt = resName.substring(0, resName.length() - ext.length());
    for (int i = 1; true; i++) {
        try {
            readResource(dbc, path + resName, CmsResourceFilter.ALL);
            resName = nameWOExt + "_" + i + ext;
        // try the next resource name with following schema: path/name_{i}.ext
        } catch (CmsVfsResourceNotFoundException e) {
            // ok, we found a not used resource name
            break;
        }
    }
    // check structure id
    CmsUUID id = structureId;
    if (getVfsDriver(dbc).validateStructureIdExists(dbc, dbc.currentProject().getUuid(), structureId)) {
        // should never happen, but if already exists create a new one
        id = new CmsUUID();
    }
    byte[] contents = null;
    boolean isFolder = true;
    // do we need the contents?
    if (histRes instanceof CmsFile) {
        contents = ((CmsFile) histRes).getContents();
        if ((contents == null) || (contents.length == 0)) {
            contents = getHistoryDriver(dbc).readContent(dbc, histRes.getResourceId(), histRes.getPublishTag());
        }
        isFolder = false;
    }
    // now read the historical properties
    List<CmsProperty> properties = getHistoryDriver(dbc).readProperties(dbc, histRes);
    // create the object to create
    CmsResource newResource = new CmsResource(id, histRes.getResourceId(), path + resName, histRes.getTypeId(), isFolder, histRes.getFlags(), dbc.currentProject().getUuid(), CmsResource.STATE_NEW, histRes.getDateCreated(), histRes.getUserCreated(), histRes.getDateLastModified(), dbc.currentUser().getId(), histRes.getDateReleased(), histRes.getDateExpired(), histRes.getSiblingCount(), histRes.getLength(), histRes.getDateContent(), histRes.getVersion());
    // log it
    log(dbc, new CmsLogEntry(dbc, newResource.getStructureId(), CmsLogEntryType.RESOURCE_RESTORE_DELETED, new String[] { newResource.getRootPath() }), false);
    // prevent the date last modified is set to the current time
    newResource.setDateLastModified(newResource.getDateLastModified());
    // restore the resource!
    CmsResource resource = createResource(dbc, path + resName, newResource, contents, properties, true);
    // set resource state to changed
    newResource.setState(CmsResource.STATE_CHANGED);
    getVfsDriver(dbc).writeResourceState(dbc, dbc.currentProject(), newResource, UPDATE_RESOURCE_STATE, false);
    newResource.setState(CmsResource.STATE_NEW);
    // fire the event
    Map<String, Object> data = new HashMap<String, Object>(2);
    data.put(I_CmsEventListener.KEY_RESOURCE, resource);
    data.put(I_CmsEventListener.KEY_CHANGE, new Integer(CHANGED_RESOURCE | CHANGED_CONTENT));
    OpenCms.fireCmsEvent(new CmsEvent(I_CmsEventListener.EVENT_RESOURCE_MODIFIED, data));
}
Also used : I_CmsHistoryResource(org.opencms.file.history.I_CmsHistoryResource) HashMap(java.util.HashMap) CmsEvent(org.opencms.main.CmsEvent) CmsVfsResourceNotFoundException(org.opencms.file.CmsVfsResourceNotFoundException) CmsFile(org.opencms.file.CmsFile) CmsResource(org.opencms.file.CmsResource) I_CmsResource(org.opencms.file.I_CmsResource) CmsProperty(org.opencms.file.CmsProperty) CmsLogEntry(org.opencms.db.log.CmsLogEntry) CmsUUID(org.opencms.util.CmsUUID) CmsObject(org.opencms.file.CmsObject)

Example 3 with I_CmsHistoryResource

use of org.opencms.file.history.I_CmsHistoryResource in project opencms-core by alkacon.

the class CmsDriverManager method restoreResource.

/**
 * Restores a resource in the current project with a version from the historical archive.<p>
 *
 * @param dbc the current database context
 * @param resource the resource to restore from the archive
 * @param version the version number to restore from the archive
 *
 * @throws CmsException if something goes wrong
 *
 * @see CmsObject#restoreResourceVersion(CmsUUID, int)
 * @see I_CmsResourceType#restoreResource(CmsObject, CmsSecurityManager, CmsResource, int)
 */
public void restoreResource(CmsDbContext dbc, CmsResource resource, int version) throws CmsException {
    I_CmsHistoryResource historyResource = readResource(dbc, resource, version);
    CmsResourceState state = CmsResource.STATE_CHANGED;
    if (resource.getState().isNew()) {
        state = CmsResource.STATE_NEW;
    }
    int newVersion = resource.getVersion();
    if (resource.getState().isUnchanged()) {
        newVersion++;
    }
    CmsResource newResource = null;
    // is the resource a file?
    if (historyResource instanceof CmsFile) {
        // get the historical up flags
        int flags = historyResource.getFlags();
        if (resource.isLabeled()) {
            // set the flag for labeled links on the restored file
            flags |= CmsResource.FLAG_LABELED;
        }
        CmsFile newFile = new CmsFile(resource.getStructureId(), resource.getResourceId(), resource.getRootPath(), historyResource.getTypeId(), flags, dbc.currentProject().getUuid(), state, resource.getDateCreated(), historyResource.getUserCreated(), resource.getDateLastModified(), dbc.currentUser().getId(), historyResource.getDateReleased(), historyResource.getDateExpired(), resource.getSiblingCount(), historyResource.getLength(), historyResource.getDateContent(), newVersion, readFile(dbc, (CmsHistoryFile) historyResource).getContents());
        // log it
        log(dbc, new CmsLogEntry(dbc, newFile.getStructureId(), CmsLogEntryType.RESOURCE_HISTORY, new String[] { newFile.getRootPath() }), false);
        newResource = writeFile(dbc, newFile);
    } else {
        // it is a folder!
        newResource = new CmsFolder(resource.getStructureId(), resource.getResourceId(), resource.getRootPath(), historyResource.getTypeId(), historyResource.getFlags(), dbc.currentProject().getUuid(), state, resource.getDateCreated(), historyResource.getUserCreated(), resource.getDateLastModified(), dbc.currentUser().getId(), historyResource.getDateReleased(), historyResource.getDateExpired(), newVersion);
        // log it
        log(dbc, new CmsLogEntry(dbc, newResource.getStructureId(), CmsLogEntryType.RESOURCE_HISTORY, new String[] { newResource.getRootPath() }), false);
        writeResource(dbc, newResource);
    }
    if (newResource != null) {
        // now read the historical properties
        List<CmsProperty> historyProperties = getHistoryDriver(dbc).readProperties(dbc, historyResource);
        // remove all properties
        deleteAllProperties(dbc, newResource.getRootPath());
        // write them to the restored resource
        writePropertyObjects(dbc, newResource, historyProperties, false);
        m_monitor.clearResourceCache();
    }
    Map<String, Object> data = new HashMap<String, Object>(2);
    data.put(I_CmsEventListener.KEY_RESOURCE, resource);
    data.put(I_CmsEventListener.KEY_CHANGE, new Integer(CHANGED_RESOURCE | CHANGED_CONTENT));
    OpenCms.fireCmsEvent(new CmsEvent(I_CmsEventListener.EVENT_RESOURCE_MODIFIED, data));
}
Also used : CmsFolder(org.opencms.file.CmsFolder) I_CmsHistoryResource(org.opencms.file.history.I_CmsHistoryResource) HashMap(java.util.HashMap) CmsEvent(org.opencms.main.CmsEvent) CmsFile(org.opencms.file.CmsFile) CmsResource(org.opencms.file.CmsResource) I_CmsResource(org.opencms.file.I_CmsResource) CmsProperty(org.opencms.file.CmsProperty) CmsLogEntry(org.opencms.db.log.CmsLogEntry) CmsObject(org.opencms.file.CmsObject)

Example 4 with I_CmsHistoryResource

use of org.opencms.file.history.I_CmsHistoryResource in project opencms-core by alkacon.

the class CmsDriverManager method readFile.

/**
 * Reads a file resource (including it's binary content) from the VFS,
 * using the specified resource filter.<p>
 *
 * In case you do not need the file content,
 * use <code>{@link #readResource(CmsDbContext, String, CmsResourceFilter)}</code> instead.<p>
 *
 * The specified filter controls what kind of resources should be "found"
 * during the read operation. This will depend on the application. For example,
 * using <code>{@link CmsResourceFilter#DEFAULT}</code> will only return currently
 * "valid" resources, while using <code>{@link CmsResourceFilter#IGNORE_EXPIRATION}</code>
 * will ignore the date release / date expired information of the resource.<p>
 *
 * @param dbc the current database context
 * @param resource the base file resource (without content)
 * @return the file read from the VFS
 * @throws CmsException if operation was not successful
 */
public CmsFile readFile(CmsDbContext dbc, CmsResource resource) throws CmsException {
    if (resource.isFolder()) {
        throw new CmsVfsResourceNotFoundException(Messages.get().container(Messages.ERR_ACCESS_FOLDER_AS_FILE_1, dbc.removeSiteRoot(resource.getRootPath())));
    }
    CmsUUID projectId = dbc.currentProject().getUuid();
    CmsFile file = null;
    if (resource instanceof I_CmsHistoryResource) {
        file = new CmsHistoryFile((I_CmsHistoryResource) resource);
        file.setContents(getHistoryDriver(dbc).readContent(dbc, resource.getResourceId(), ((I_CmsHistoryResource) resource).getPublishTag()));
    } else {
        file = new CmsFile(resource);
        file.setContents(getVfsDriver(dbc).readContent(dbc, projectId, resource.getResourceId()));
    }
    return file;
}
Also used : CmsVfsResourceNotFoundException(org.opencms.file.CmsVfsResourceNotFoundException) CmsFile(org.opencms.file.CmsFile) I_CmsHistoryResource(org.opencms.file.history.I_CmsHistoryResource) CmsHistoryFile(org.opencms.file.history.CmsHistoryFile) CmsUUID(org.opencms.util.CmsUUID)

Example 5 with I_CmsHistoryResource

use of org.opencms.file.history.I_CmsHistoryResource in project opencms-core by alkacon.

the class CmsDriverManager method deleteHistoricalVersions.

/**
 * Deletes the versions from the history tables, keeping the given number of versions per resource.<p>
 *
 * if the <code>cleanUp</code> option is set, additionally versions of deleted resources will be removed.<p>
 *
 * @param dbc the current database context
 * @param versionsToKeep number of versions to keep, is ignored if negative
 * @param versionsDeleted number of versions to keep for deleted resources, is ignored if negative
 * @param timeDeleted deleted resources older than this will also be deleted, is ignored if negative
 * @param report the report for output logging
 *
 * @throws CmsException if operation was not successful
 */
public void deleteHistoricalVersions(CmsDbContext dbc, int versionsToKeep, int versionsDeleted, long timeDeleted, I_CmsReport report) throws CmsException {
    report.println(Messages.get().container(Messages.RPT_START_DELETE_VERSIONS_0), I_CmsReport.FORMAT_HEADLINE);
    if (versionsToKeep >= 0) {
        report.println(Messages.get().container(Messages.RPT_START_DELETE_ACT_VERSIONS_1, new Integer(versionsToKeep)), I_CmsReport.FORMAT_HEADLINE);
        List<I_CmsHistoryResource> resources = getHistoryDriver(dbc).getAllNotDeletedEntries(dbc);
        if (resources.isEmpty()) {
            report.println(Messages.get().container(Messages.RPT_DELETE_NOTHING_0), I_CmsReport.FORMAT_OK);
        }
        int n = resources.size();
        int m = 1;
        Iterator<I_CmsHistoryResource> itResources = resources.iterator();
        while (itResources.hasNext()) {
            I_CmsHistoryResource histResource = itResources.next();
            report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_SUCCESSION_2, String.valueOf(m), String.valueOf(n)), I_CmsReport.FORMAT_NOTE);
            report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_ARGUMENT_1, dbc.removeSiteRoot(histResource.getRootPath())));
            report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
            try {
                int deleted = getHistoryDriver(dbc).deleteEntries(dbc, histResource, versionsToKeep, -1);
                report.print(Messages.get().container(Messages.RPT_VERSION_DELETING_1, new Integer(deleted)), I_CmsReport.FORMAT_NOTE);
                report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
                report.println(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0), I_CmsReport.FORMAT_OK);
            } catch (CmsDataAccessException e) {
                report.println(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_ERROR_0), I_CmsReport.FORMAT_ERROR);
                if (LOG.isDebugEnabled()) {
                    LOG.debug(e.getLocalizedMessage(), e);
                }
            }
            m++;
        }
        report.println(Messages.get().container(Messages.RPT_END_DELETE_ACT_VERSIONS_0), I_CmsReport.FORMAT_HEADLINE);
    }
    if ((versionsDeleted >= 0) || (timeDeleted >= 0)) {
        if (timeDeleted >= 0) {
            report.println(Messages.get().container(Messages.RPT_START_DELETE_DEL_VERSIONS_2, new Integer(versionsDeleted), new Date(timeDeleted)), I_CmsReport.FORMAT_HEADLINE);
        } else {
            report.println(Messages.get().container(Messages.RPT_START_DELETE_DEL_VERSIONS_1, new Integer(versionsDeleted)), I_CmsReport.FORMAT_HEADLINE);
        }
        List<I_CmsHistoryResource> resources = getHistoryDriver(dbc).getAllDeletedEntries(dbc);
        if (resources.isEmpty()) {
            report.println(Messages.get().container(Messages.RPT_DELETE_NOTHING_0), I_CmsReport.FORMAT_OK);
        }
        int n = resources.size();
        int m = 1;
        Iterator<I_CmsHistoryResource> itResources = resources.iterator();
        while (itResources.hasNext()) {
            I_CmsHistoryResource histResource = itResources.next();
            report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_SUCCESSION_2, String.valueOf(m), String.valueOf(n)), I_CmsReport.FORMAT_NOTE);
            report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_ARGUMENT_1, dbc.removeSiteRoot(histResource.getRootPath())));
            report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
            try {
                int deleted = getHistoryDriver(dbc).deleteEntries(dbc, histResource, versionsDeleted, timeDeleted);
                report.print(Messages.get().container(Messages.RPT_VERSION_DELETING_1, new Integer(deleted)), I_CmsReport.FORMAT_NOTE);
                report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
                report.println(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0), I_CmsReport.FORMAT_OK);
            } catch (CmsDataAccessException e) {
                report.println(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_ERROR_0), I_CmsReport.FORMAT_ERROR);
                if (LOG.isDebugEnabled()) {
                    LOG.debug(e.getLocalizedMessage(), e);
                }
            }
            m++;
        }
        report.println(Messages.get().container(Messages.RPT_END_DELETE_DEL_VERSIONS_0), I_CmsReport.FORMAT_HEADLINE);
    }
    report.println(Messages.get().container(Messages.RPT_END_DELETE_VERSIONS_0), I_CmsReport.FORMAT_HEADLINE);
}
Also used : I_CmsHistoryResource(org.opencms.file.history.I_CmsHistoryResource) CmsDataAccessException(org.opencms.file.CmsDataAccessException) Date(java.util.Date)

Aggregations

I_CmsHistoryResource (org.opencms.file.history.I_CmsHistoryResource)48 ArrayList (java.util.ArrayList)16 CmsResource (org.opencms.file.CmsResource)13 List (java.util.List)12 SQLException (java.sql.SQLException)10 CmsFile (org.opencms.file.CmsFile)10 CmsVfsResourceNotFoundException (org.opencms.file.CmsVfsResourceNotFoundException)10 CmsUUID (org.opencms.util.CmsUUID)10 Connection (java.sql.Connection)9 PreparedStatement (java.sql.PreparedStatement)9 CmsDbSqlException (org.opencms.db.CmsDbSqlException)9 CmsException (org.opencms.main.CmsException)9 ResultSet (java.sql.ResultSet)7 CmsObject (org.opencms.file.CmsObject)7 HashMap (java.util.HashMap)6 I_CmsResource (org.opencms.file.I_CmsResource)6 CmsHistoryFile (org.opencms.file.history.CmsHistoryFile)6 CmsProperty (org.opencms.file.CmsProperty)5 Map (java.util.Map)4 CmsDataAccessException (org.opencms.file.CmsDataAccessException)4