public void createSibling(CmsDbContext dbc, CmsProject project, CmsResource resource)
    throws CmsDataAccessException {

        if (!project.getUuid().equals(CmsProject.ONLINE_PROJECT_ID)) {
            // this method is only intended to be used during publishing
            return;
        }

        // check if the resource already exists
        CmsResource existingSibling = null;
        CmsUUID newStructureId = resource.getStructureId();

        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            existingSibling = readResource(dbc, project.getUuid(), resource.getRootPath(), true);

            if (existingSibling.getState().isDeleted()) {
                // if an existing resource is deleted, it will be finally removed now.
                // but we have to reuse its id in order to avoid orphans in the online project.
                newStructureId = existingSibling.getStructureId();

                // remove the existing file and it's properties
                List<CmsResource> modifiedResources = readSiblings(dbc, project.getUuid(), existingSibling, false);
                int propertyDeleteOption = (existingSibling.getSiblingCount() > 1)
                ? CmsProperty.DELETE_OPTION_DELETE_STRUCTURE_VALUES
                : CmsProperty.DELETE_OPTION_DELETE_STRUCTURE_AND_RESOURCE_VALUES;
                deletePropertyObjects(dbc, project.getUuid(), existingSibling, propertyDeleteOption);
                removeFile(dbc, project.getUuid(), existingSibling);

                OpenCms.fireCmsEvent(
                    new CmsEvent(
                        I_CmsEventListener.EVENT_RESOURCES_MODIFIED,
                        Collections.<String, Object> singletonMap(
                            I_CmsEventListener.KEY_RESOURCES,
                            modifiedResources)));
                OpenCms.fireCmsEvent(
                    new CmsEvent(
                        I_CmsEventListener.EVENT_RESOURCE_AND_PROPERTIES_MODIFIED,
                        Collections.<String, Object> singletonMap(I_CmsEventListener.KEY_RESOURCE, existingSibling)));
            } else {
                // we have a collision: there exists already a resource with the same path/name which could not be removed
                throw new CmsVfsResourceAlreadyExistsException(
                    Messages.get().container(
                        Messages.ERR_RESOURCE_WITH_NAME_ALREADY_EXISTS_1,
                        dbc.removeSiteRoot(resource.getRootPath())));
            }
        } catch (CmsVfsResourceNotFoundException e) {
            // that's what we want in the best case- anything else should be thrown
        }

        // check if a resource with the specified ID already exists
        if (!validateResourceIdExists(dbc, project.getUuid(), resource.getResourceId())) {
            throw new CmsVfsResourceNotFoundException(
                Messages.get().container(
                    Messages.ERR_CREATE_SIBLING_FILE_NOT_FOUND_1,
                    dbc.removeSiteRoot(resource.getRootPath())));
        }

        // write a new structure referring to the resource
        try {
            // use consistent version numbers if the file is being restored
            int lastVersion = m_driverManager.getHistoryDriver(dbc).readLastVersion(dbc, newStructureId);
            int newStrVersion = 0;
            if (lastVersion > 0) {
                I_CmsHistoryResource histRes = m_driverManager.getHistoryDriver(dbc).readResource(
                    dbc,
                    newStructureId,
                    lastVersion);
                newStrVersion = histRes.getStructureVersion();
            }

            // read the parent id
            String parentId = internalReadParentId(dbc, project.getUuid(), resource.getRootPath());

            conn = m_sqlManager.getConnection(dbc);

            // write the structure
            stmt = m_sqlManager.getPreparedStatement(conn, project, "C_STRUCTURE_WRITE");
            stmt.setString(1, newStructureId.toString());
            stmt.setString(2, resource.getResourceId().toString());
            stmt.setString(3, resource.getRootPath());
            stmt.setInt(4, CmsResource.STATE_UNCHANGED.getState());
            stmt.setLong(5, resource.getDateReleased());
            stmt.setLong(6, resource.getDateExpired());
            stmt.setString(7, parentId);
            stmt.setInt(8, newStrVersion); // initial structure version number
            stmt.executeUpdate();
            m_sqlManager.closeAll(dbc, conn, stmt, null);

            int sibCount = countSiblings(dbc, project.getUuid(), resource.getResourceId());
            conn = m_sqlManager.getConnection(dbc);

            // update the link Count
            stmt = m_sqlManager.getPreparedStatement(conn, project, "C_RESOURCES_UPDATE_SIBLING_COUNT");
            stmt.setInt(1, sibCount);
            stmt.setString(2, resource.getResourceId().toString());
            stmt.executeUpdate();

            m_sqlManager.closeAll(dbc, null, stmt, null);

            // update the project last modified and flags
            stmt = m_sqlManager.getPreparedStatement(conn, project, "C_RESOURCES_UPDATE_RESOURCE_PROJECT");
            stmt.setInt(1, resource.getFlags());
            stmt.setString(2, resource.getProjectLastModified().toString());
            stmt.setString(3, resource.getResourceId().toString());
            stmt.executeUpdate();
        } catch (SQLException e) {
            throw new CmsDbSqlException(
                Messages.get().container(Messages.ERR_GENERIC_SQL_1, CmsDbSqlException.getErrorQuery(stmt)),
                e);
        } finally {
            m_sqlManager.closeAll(dbc, conn, stmt, null);
        }
        repairBrokenRelations(dbc, project.getUuid(), resource.getStructureId(), resource.getRootPath());
    }
public void createSibling ( CmsDbContext dbc , CmsProject project , CmsResource resource ) throws CmsDataAccessException { if ( ! project . getUuid ( ) . equals ( CmsProject . ONLINE_PROJECT_ID ) ) { return ; } CmsResource existingSibling = null ; CmsUUID newStructureId = resource . getStructureId ( ) ; Connection conn = null ; PreparedStatement stmt = null ; try { existingSibling = readResource ( dbc , project . getUuid ( ) , resource . getRootPath ( ) , true ) ; if ( existingSibling . getState ( ) . isDeleted ( ) ) { newStructureId = existingSibling . getStructureId ( ) ; List < CmsResource > modifiedResources = readSiblings ( dbc , project . getUuid ( ) , existingSibling , false ) ; int propertyDeleteOption = ( existingSibling . getSiblingCount ( ) > 1 ) ? CmsProperty . DELETE_OPTION_DELETE_STRUCTURE_VALUES : CmsProperty . DELETE_OPTION_DELETE_STRUCTURE_AND_RESOURCE_VALUES ; deletePropertyObjects ( dbc , project . getUuid ( ) , existingSibling , propertyDeleteOption ) ; removeFile ( dbc , project . getUuid ( ) , existingSibling ) ; OpenCms . fireCmsEvent ( new CmsEvent ( I_CmsEventListener . EVENT_RESOURCES_MODIFIED , Collections . < String , Object > singletonMap ( I_CmsEventListener . KEY_RESOURCES , modifiedResources ) ) ) ; OpenCms . fireCmsEvent ( new CmsEvent ( I_CmsEventListener . EVENT_RESOURCE_AND_PROPERTIES_MODIFIED , Collections . < String , Object > singletonMap ( I_CmsEventListener . KEY_RESOURCE , existingSibling ) ) ) ; } else { throw new CmsVfsResourceAlreadyExistsException ( Messages . get ( ) . container ( Messages . ERR_RESOURCE_WITH_NAME_ALREADY_EXISTS_1 , dbc . removeSiteRoot ( resource . getRootPath ( ) ) ) ) ; } } catch ( CmsVfsResourceNotFoundException e ) { } if ( ! validateResourceIdExists ( dbc , project . getUuid ( ) , resource . getResourceId ( ) ) ) { throw new CmsVfsResourceNotFoundException ( Messages . get ( ) . container ( Messages . ERR_CREATE_SIBLING_FILE_NOT_FOUND_1 , dbc . removeSiteRoot ( resource . getRootPath ( ) ) ) ) ; } try { int lastVersion = m_driverManager . getHistoryDriver ( dbc ) . readLastVersion ( dbc , newStructureId ) ; int newStrVersion = 0 ; if ( lastVersion > 0 ) { I_CmsHistoryResource histRes = m_driverManager . getHistoryDriver ( dbc ) . readResource ( dbc , newStructureId , lastVersion ) ; newStrVersion = histRes . getStructureVersion ( ) ; } String parentId = internalReadParentId ( dbc , project . getUuid ( ) , resource . getRootPath ( ) ) ; conn = m_sqlManager . getConnection ( dbc ) ; stmt = m_sqlManager . getPreparedStatement ( conn , project , "C_STRUCTURE_WRITE" ) ; stmt . setString ( 1 , newStructureId . toString ( ) ) ; stmt . setString ( 2 , resource . getResourceId ( ) . toString ( ) ) ; stmt . setString ( 3 , resource . getRootPath ( ) ) ; stmt . setInt ( 4 , CmsResource . STATE_UNCHANGED . getState ( ) ) ; stmt . setLong ( 5 , resource . getDateReleased ( ) ) ; stmt . setLong ( 6 , resource . getDateExpired ( ) ) ; stmt . setString ( 7 , parentId ) ; stmt . setInt ( 8 , newStrVersion ) ; stmt . executeUpdate ( ) ; m_sqlManager . closeAll ( dbc , conn , stmt , null ) ; int sibCount = countSiblings ( dbc , project . getUuid ( ) , resource . getResourceId ( ) ) ; conn = m_sqlManager . getConnection ( dbc ) ; stmt = m_sqlManager . getPreparedStatement ( conn , project , "C_RESOURCES_UPDATE_SIBLING_COUNT" ) ; stmt . setInt ( 1 , sibCount ) ; stmt . setString ( 2 , resource . getResourceId ( ) . toString ( ) ) ; stmt . executeUpdate ( ) ; m_sqlManager . closeAll ( dbc , null , stmt , null ) ; stmt = m_sqlManager . getPreparedStatement ( conn , project , "C_RESOURCES_UPDATE_RESOURCE_PROJECT" ) ; stmt . setInt ( 1 , resource . getFlags ( ) ) ; stmt . setString ( 2 , resource . getProjectLastModified ( ) . toString ( ) ) ; stmt . setString ( 3 , resource . getResourceId ( ) . toString ( ) ) ; stmt . executeUpdate ( ) ; } catch ( SQLException e ) { throw new CmsDbSqlException ( Messages . get ( ) . container ( Messages . ERR_GENERIC_SQL_1 , CmsDbSqlException . getErrorQuery ( stmt ) ) , e ) ; } finally { m_sqlManager . closeAll ( dbc , conn , stmt , null ) ; } repairBrokenRelations ( dbc , project . getUuid ( ) , resource . getStructureId ( ) , resource . getRootPath ( ) ) ; }
@see org.opencms.db.I_CmsVfsDriver#createSibling(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.file.CmsResource)