public List<I_CmsHistoryResource> readAllAvailableVersions(CmsDbContext dbc, CmsUUID structureId)
    throws CmsDataAccessException {

        ResultSet res = null;
        List<I_CmsHistoryResource> result = new ArrayList<I_CmsHistoryResource>();
        PreparedStatement stmt = null;
        Connection conn = null;

        try {
            conn = m_sqlManager.getConnection(dbc);

            // get all direct versions (where the structure entry has been written)
            // sorted from the NEWEST to the OLDEST version (publish tag descendant)
            List<I_CmsHistoryResource> historyResources = new ArrayList<I_CmsHistoryResource>();
            stmt = m_sqlManager.getPreparedStatement(conn, "C_RESOURCES_HISTORY_READ_ALL_VERSIONS");
            stmt.setString(1, structureId.toString());
            res = stmt.executeQuery();
            while (res.next()) {
                historyResources.add(internalCreateResource(res));
            }
            m_sqlManager.closeAll(dbc, null, stmt, res);

            if (!historyResources.isEmpty()) {
                // look for newer versions
                // this is the NEWEST version, with the HIGHEST publish tag
                I_CmsHistoryResource histRes = historyResources.get(0);

                // look for later resource entries
                stmt = m_sqlManager.getPreparedStatement(conn, "C_RESOURCES_HISTORY_READ_NEW_VERSIONS");
                stmt.setString(1, histRes.getResourceId().toString());
                stmt.setInt(2, histRes.getPublishTag());
                res = stmt.executeQuery();

                I_CmsHistoryResource lastHistRes = histRes;
                // these are sorted from the oldest to the newest version (publish tag ascendent)
                while (res.next()) {
                    int resVersion = res.getInt(m_sqlManager.readQuery("C_RESOURCES_VERSION"));
                    if (resVersion == lastHistRes.getResourceVersion()) {
                        // skip not interesting versions
                        continue;
                    }
                    I_CmsHistoryResource newHistRes = internalMergeResource(histRes, res, 0);
                    // add interesting versions, in the right order
                    result.add(0, newHistRes);
                    lastHistRes = newHistRes;
                }
                m_sqlManager.closeAll(dbc, null, stmt, res);
            }
            // iterate from the NEWEST to the OLDEST versions (publish tag descendant)
            for (int i = 0; i < historyResources.size(); i++) {
                I_CmsHistoryResource histRes = historyResources.get(i);
                result.add(histRes);
                if (i < (historyResources.size() - 1)) {
                    // this is one older direct version than histRes (histRes.getPublishTag() > histRes2.getPublishTag())
                    I_CmsHistoryResource histRes2 = historyResources.get(i + 1);

                    // look for resource changes in between of the direct versions in ascendent order
                    stmt = m_sqlManager.getPreparedStatement(conn, "C_RESOURCES_HISTORY_READ_BTW_VERSIONS");
                    stmt.setString(1, histRes.getResourceId().toString());
                    stmt.setInt(2, histRes2.getPublishTag()); // lower limit
                    stmt.setInt(3, histRes.getPublishTag()); // upper limit
                    res = stmt.executeQuery();

                    int pos = result.size();
                    I_CmsHistoryResource lastHistRes = histRes2;
                    while (res.next()) {
                        int resVersion = res.getInt(m_sqlManager.readQuery("C_RESOURCES_VERSION"));
                        if (resVersion == lastHistRes.getResourceVersion()) {
                            // skip not interesting versions
                            continue;
                        }
                        I_CmsHistoryResource newHistRes = internalMergeResource(histRes2, res, 0);
                        // add interesting versions, in the right order
                        result.add(pos, newHistRes);
                        lastHistRes = newHistRes;
                    }
                    m_sqlManager.closeAll(dbc, null, stmt, res);
                }
            }
            if (!result.isEmpty()) {
                // get the oldest version
                I_CmsHistoryResource histRes = result.get(result.size() - 1);

                if (histRes.getVersion() > 1) {
                    // look for older resource versions, in descendant order
                    stmt = m_sqlManager.getPreparedStatement(conn, "C_RESOURCES_HISTORY_READ_OLD_VERSIONS");
                    stmt.setString(1, histRes.getResourceId().toString());
                    stmt.setInt(2, histRes.getPublishTag());
                    res = stmt.executeQuery();

                    int offset = (histRes.getStructureVersion() > 0 ? 1 : 0);

                    I_CmsHistoryResource lastHistRes = histRes;
                    while (res.next()) {
                        I_CmsHistoryResource newHistRes = internalMergeResource(histRes, res, offset);
                        if (newHistRes.getResourceVersion() != lastHistRes.getResourceVersion()) {
                            // only add interesting versions
                            if (offset == 1) {
                                if (histRes != lastHistRes) {
                                    result.add(lastHistRes);
                                }
                            } else {
                                result.add(newHistRes);
                            }
                        }
                        lastHistRes = newHistRes;
                    }
                    // add the last one if there is one
                    if ((offset == 1) && (lastHistRes != histRes)) {
                        result.add(lastHistRes);
                    }
                    m_sqlManager.closeAll(dbc, null, stmt, res);
                }
            }
        } 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, res);
        }

        return result;
    }
public List < I_CmsHistoryResource > readAllAvailableVersions ( CmsDbContext dbc , CmsUUID structureId ) throws CmsDataAccessException { ResultSet res = null ; List < I_CmsHistoryResource > result = new ArrayList < I_CmsHistoryResource > ( ) ; PreparedStatement stmt = null ; Connection conn = null ; try { conn = m_sqlManager . getConnection ( dbc ) ; List < I_CmsHistoryResource > historyResources = new ArrayList < I_CmsHistoryResource > ( ) ; stmt = m_sqlManager . getPreparedStatement ( conn , "C_RESOURCES_HISTORY_READ_ALL_VERSIONS" ) ; stmt . setString ( 1 , structureId . toString ( ) ) ; res = stmt . executeQuery ( ) ; while ( res . next ( ) ) { historyResources . add ( internalCreateResource ( res ) ) ; } m_sqlManager . closeAll ( dbc , null , stmt , res ) ; if ( ! historyResources . isEmpty ( ) ) { I_CmsHistoryResource histRes = historyResources . get ( 0 ) ; stmt = m_sqlManager . getPreparedStatement ( conn , "C_RESOURCES_HISTORY_READ_NEW_VERSIONS" ) ; stmt . setString ( 1 , histRes . getResourceId ( ) . toString ( ) ) ; stmt . setInt ( 2 , histRes . getPublishTag ( ) ) ; res = stmt . executeQuery ( ) ; I_CmsHistoryResource lastHistRes = histRes ; while ( res . next ( ) ) { int resVersion = res . getInt ( m_sqlManager . readQuery ( "C_RESOURCES_VERSION" ) ) ; if ( resVersion == lastHistRes . getResourceVersion ( ) ) { continue ; } I_CmsHistoryResource newHistRes = internalMergeResource ( histRes , res , 0 ) ; result . add ( 0 , newHistRes ) ; lastHistRes = newHistRes ; } m_sqlManager . closeAll ( dbc , null , stmt , res ) ; } for ( int i = 0 ; i < historyResources . size ( ) ; i ++ ) { I_CmsHistoryResource histRes = historyResources . get ( i ) ; result . add ( histRes ) ; if ( i < ( historyResources . size ( ) - 1 ) ) { I_CmsHistoryResource histRes2 = historyResources . get ( i + 1 ) ; stmt = m_sqlManager . getPreparedStatement ( conn , "C_RESOURCES_HISTORY_READ_BTW_VERSIONS" ) ; stmt . setString ( 1 , histRes . getResourceId ( ) . toString ( ) ) ; stmt . setInt ( 2 , histRes2 . getPublishTag ( ) ) ; stmt . setInt ( 3 , histRes . getPublishTag ( ) ) ; res = stmt . executeQuery ( ) ; int pos = result . size ( ) ; I_CmsHistoryResource lastHistRes = histRes2 ; while ( res . next ( ) ) { int resVersion = res . getInt ( m_sqlManager . readQuery ( "C_RESOURCES_VERSION" ) ) ; if ( resVersion == lastHistRes . getResourceVersion ( ) ) { continue ; } I_CmsHistoryResource newHistRes = internalMergeResource ( histRes2 , res , 0 ) ; result . add ( pos , newHistRes ) ; lastHistRes = newHistRes ; } m_sqlManager . closeAll ( dbc , null , stmt , res ) ; } } if ( ! result . isEmpty ( ) ) { I_CmsHistoryResource histRes = result . get ( result . size ( ) - 1 ) ; if ( histRes . getVersion ( ) > 1 ) { stmt = m_sqlManager . getPreparedStatement ( conn , "C_RESOURCES_HISTORY_READ_OLD_VERSIONS" ) ; stmt . setString ( 1 , histRes . getResourceId ( ) . toString ( ) ) ; stmt . setInt ( 2 , histRes . getPublishTag ( ) ) ; res = stmt . executeQuery ( ) ; int offset = ( histRes . getStructureVersion ( ) > 0 ? 1 : 0 ) ; I_CmsHistoryResource lastHistRes = histRes ; while ( res . next ( ) ) { I_CmsHistoryResource newHistRes = internalMergeResource ( histRes , res , offset ) ; if ( newHistRes . getResourceVersion ( ) != lastHistRes . getResourceVersion ( ) ) { if ( offset == 1 ) { if ( histRes != lastHistRes ) { result . add ( lastHistRes ) ; } } else { result . add ( newHistRes ) ; } } lastHistRes = newHistRes ; } if ( ( offset == 1 ) && ( lastHistRes != histRes ) ) { result . add ( lastHistRes ) ; } m_sqlManager . closeAll ( dbc , null , stmt , res ) ; } } } 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 , res ) ; } return result ; }
@see org.opencms.db.I_CmsHistoryDriver#readAllAvailableVersions(CmsDbContext, CmsUUID)