<commit_before>package org.apache.maven.continuum.store;

/*
 * Copyright 2004-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.apache.maven.continuum.model.project.BuildResult;
import org.apache.maven.continuum.model.project.Profile;
import org.apache.maven.continuum.model.project.Project;
import org.apache.maven.continuum.model.project.ProjectGroup;
import org.apache.maven.continuum.model.project.Schedule;
import org.apache.maven.continuum.model.system.Installation;
import org.apache.maven.continuum.project.ContinuumBuild;
import org.apache.maven.continuum.project.ContinuumBuildSettings;
import org.apache.maven.continuum.project.ContinuumNotifier;
import org.apache.maven.continuum.project.ContinuumProject;
import org.apache.maven.continuum.project.ContinuumProjectGroup;
import org.apache.maven.continuum.project.ContinuumProjectState;
import org.apache.maven.continuum.project.ContinuumSchedule;
import org.apache.maven.continuum.scm.ScmResult;
import org.codehaus.plexus.jdo.JdoFactory;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;

import javax.jdo.Extent;
import javax.jdo.JDOHelper;
import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.JDOUserException;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import javax.jdo.Transaction;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * @author <a href="mailto:<EMAIL>"><NAME>&oslash;l</a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 * @todo cleanup old stuff
 */
public class JdoContinuumStore
    extends AbstractContinuumStore
    implements ContinuumStore, Initializable
{
    /**
     * @plexus.requirement
     */
    private JdoFactory jdoFactory;

    // ----------------------------------------------------------------------
    //
    // ----------------------------------------------------------------------

    private PersistenceManagerFactory pmf;

    // ----------------------------------------------------------------------
    // Fetch Groups
    // ----------------------------------------------------------------------

    private static final String PROJECT_DETAIL_FG = "project-detail";

    private static final String BUILD_DETAIL_FG = "build-detail";

    private static final String PROJECT_GROUP_DETAIL_FG = "project-group-detail";

    private static final String SCHEDULE_DETAIL_FG = "schedule-detail";

    private static final String BUILD_SETTINGS_DETAIL_FG = "build-settings-detail";

    private static final String PROJECT_WITH_BUILDS_FETCH_GROUP = "project-with-builds";

    private static final String PROJECT_WITH_CHECKOUT_RESULT_FETCH_GROUP = "project-with-checkout-result";

    private static final String BUILD_RESULT_WITH_DETAILS_FETCH_GROUP = "build-result-with-details";

    private static final String PROJECT_BUILD_DETAILS_FETCH_GROUP = "project-build-details";

    private static final String PROJECT_ALL_DETAILS_FETCH_GROUP = "project-all-details";

    // ----------------------------------------------------------------------
    // Component Lifecycle
    // ----------------------------------------------------------------------

    public void initialize()
        throws InitializationException
    {
        pmf = jdoFactory.getPersistenceManagerFactory();
    }

    // ----------------------------------------------------------------------
    // ContinuumStore Implementation
    // ----------------------------------------------------------------------

    public ContinuumProject addProject( ContinuumProject project )
        throws ContinuumStoreException
    {
        return (ContinuumProject) addObject( project, PROJECT_DETAIL_FG );
    }

    public void removeProject( String projectId )
        throws ContinuumStoreException
    {
        PersistenceManager pm = pmf.getPersistenceManager();

        Transaction tx = pm.currentTransaction();

        try
        {
            tx.begin();

            Object id = pm.newObjectIdInstance( ContinuumProject.class, projectId );

            ContinuumProject project = (ContinuumProject) pm.getObjectById( id );

            // ----------------------------------------------------------------------
            // We need to remove this project reference from any schedule in the
            // system. So grab the list of schedules this project belongs to
            // then iterate through the collection of schedules removing the
            // reference to this project. This seems like a bit much but the
            // only thing that works.
            // ----------------------------------------------------------------------

            if ( project.getSchedules() != null && project.getSchedules().size() > 0 )
            {
                Set schedules = project.getSchedules();

                for ( Iterator i = schedules.iterator(); i.hasNext(); )
                {
                    ContinuumSchedule schedule = (ContinuumSchedule) i.next();

                    schedule.getProjects().remove( project );
                }
            }

            if ( project.getProjectGroup() != null )
            {
                ContinuumProjectGroup pg = project.getProjectGroup();

                pg.getProjects().remove( project );
            }

            pm.deletePersistent( project );

            tx.commit();
        }
        finally
        {
            rollback( tx );
        }
    }

    public ContinuumProject updateProject( ContinuumProject project )
        throws ContinuumStoreException
    {
        String checkoutErrorMessage = project.getCheckOutErrorMessage();

        String checkoutErrorException = project.getCheckOutErrorException();

        if ( checkoutErrorMessage != null && checkoutErrorMessage.length() > 255 )
        {
            project.setCheckOutErrorMessage( checkoutErrorMessage.substring( 0, 255 ) );
        }

        if ( checkoutErrorException != null && checkoutErrorException.length() > 255 )
        {
            project.setCheckOutErrorException( checkoutErrorException.substring( 0, 255 ) );
        }

        // ----------------------------------------------------------------------
        //
        // ----------------------------------------------------------------------

        PersistenceManager pm = pmf.getPersistenceManager();

        Transaction tx = pm.currentTransaction();

        try
        {
            tx.begin();

            // ----------------------------------------------------------------------
            // Work around for bug with M:N relationships. We must persist the list
            // of schedules or they don't get saved.
            // ----------------------------------------------------------------------

//            if ( project.getSchedules() != null && project.getSchedules().size() > 0 )
//            {
//                pm.attachCopyAll( project.getSchedules(), true );
//            }
//
//            if ( project.getProjectGroup() != null )
//            {
//                pm.attachCopy( project.getProjectGroup(), true );
//            }
//
//            if ( project.getScmResult() != null )
//            {
//                pm.attachCopy( project.getScmResult(), true );
//            }

            pm.attachCopy( project, true );

            tx.commit();

            return (ContinuumProject) getDetailedObject( ContinuumProject.class, project.getId(), PROJECT_DETAIL_FG );
        }
        finally
        {
            rollback( tx );
        }
    }

    public Collection getAllProjects()
        throws ContinuumStoreException
    {
        PersistenceManager pm = pmf.getPersistenceManager();

        Transaction tx = pm.currentTransaction();

        try
        {
            tx.begin();

            Extent extent = pm.getExtent( ContinuumProject.class, true );

            Query query = pm.newQuery( extent );

            query.setOrdering( "name ascending" );

            Collection result = (Collection) query.execute();

            result = pm.detachCopyAll( result );

            for ( Iterator it = result.iterator(); it.hasNext(); )
            {
                setProjectState( (ContinuumProject) it.next() );
            }

            tx.commit();

            return result;
        }
        finally
        {
            rollback( tx );
        }
    }

    public ContinuumProject getProjectByName( String name )
        throws ContinuumStoreException
    {
        PersistenceManager pm = pmf.getPersistenceManager();

        Transaction tx = pm.currentTransaction();

        try
        {
            tx.begin();

            Extent extent = pm.getExtent( ContinuumProject.class, true );

            Query query = pm.newQuery( extent );

            query.declareImports( "import java.lang.String" );

            query.declareParameters( "String name" );

            query.setFilter( "this.name == name" );

            query.getFetchPlan().addGroup( PROJECT_DETAIL_FG );

            Collection result = (Collection) query.execute( name );

            if ( result.size() == 0 )
            {
                tx.commit();

                return null;
            }

            Object object = pm.detachCopy( result.iterator().next() );

            tx.commit();

            return (ContinuumProject) object;
        }
        finally
        {
            rollback( tx );
        }
    }

    public ContinuumProject getProject( String projectId )
        throws ContinuumStoreException
    {
        PersistenceManager pm = pmf.getPersistenceManager();

        Transaction tx = pm.currentTransaction();

        try
        {
            tx.begin();

            ContinuumProject project = getContinuumProject( pm, projectId, true );

            project = (ContinuumProject) pm.detachCopy( project );

            tx.commit();

            return project;
        }
        catch ( JDOObjectNotFoundException e )
        {
            throw new ContinuumObjectNotFoundException( ContinuumProject.class.getName(), projectId );
        }
        finally
        {
            rollback( tx );
        }
    }

    public ContinuumProject getProjectForBuild( String buildId )
        throws ContinuumStoreException
    {
        PersistenceManager pm = pmf.getPersistenceManager();

        Transaction tx = pm.currentTransaction();

        try
        {
            tx.begin();

            ContinuumBuild build = getContinuumBuild( pm, buildId );

            String projectId = build.getProject().getId();

            ContinuumProject project = getContinuumProject( pm, projectId, true );

            tx.commit();

            return project;
        }
        finally
        {
            rollback( tx );
        }
    }

    public ScmResult getScmResultForProject( String projectId )
        throws ContinuumStoreException
    {
        PersistenceManager pm = pmf.getPersistenceManager();

        Transaction tx = pm.currentTransaction();

        try
        {
            tx.begin();

            ContinuumProject project = getContinuumProject( pm, projectId, false );

            ScmResult scmResult = project.getCheckoutResult();

            if ( scmResult == null )
            {
                tx.commit();

                return null;
            }

            scmResult = (ScmResult) pm.detachCopy( scmResult );

            tx.commit();

            return scmResult;<commit_msg>rolling back extra change for now<commit_after>package org.apache.maven.continuum.store;

/*
 * Copyright 2004-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.apache.maven.continuum.model.project.BuildResult;
import org.apache.maven.continuum.model.project.Profile;
import org.apache.maven.continuum.model.project.Project;
import org.apache.maven.continuum.model.project.ProjectGroup;
import org.apache.maven.continuum.model.project.Schedule;
import org.apache.maven.continuum.model.system.Installation;
import org.apache.maven.continuum.project.ContinuumBuild;
import org.apache.maven.continuum.project.ContinuumBuildSettings;
import org.apache.maven.continuum.project.ContinuumNotifier;
import org.apache.maven.continuum.project.ContinuumProject;
import org.apache.maven.continuum.project.ContinuumProjectGroup;
import org.apache.maven.continuum.project.ContinuumProjectState;
import org.apache.maven.continuum.project.ContinuumSchedule;
import org.apache.maven.continuum.scm.ScmResult;
import org.codehaus.plexus.jdo.JdoFactory;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;

import javax.jdo.Extent;
import javax.jdo.JDOHelper;
import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.JDOUserException;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import javax.jdo.Transaction;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * @author <a href="mailto:<EMAIL>"><NAME>&oslash;l</a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 * @todo cleanup old stuff
 */
public class JdoContinuumStore
    extends AbstractContinuumStore
    implements ContinuumStore, Initializable
{
    /**
     * @plexus.requirement
     */
    private JdoFactory jdoFactory;

    // ----------------------------------------------------------------------
    //
    // ----------------------------------------------------------------------

    private PersistenceManagerFactory pmf;

    // ----------------------------------------------------------------------
    // Fetch Groups
    // ----------------------------------------------------------------------

    private static final String PROJECT_DETAIL_FG = "project-detail";

    private static final String BUILD_DETAIL_FG = "build-detail";

    private static final String PROJECT_GROUP_DETAIL_FG = "project-group-detail";

    private static final String SCHEDULE_DETAIL_FG = "schedule-detail";

    private static final String BUILD_SETTINGS_DETAIL_FG = "build-settings-detail";

    private static final String PROJECT_WITH_BUILDS_FETCH_GROUP = "project-with-builds";

    private static final String PROJECT_WITH_CHECKOUT_RESULT_FETCH_GROUP = "project-with-checkout-result";

    private static final String BUILD_RESULT_WITH_DETAILS_FETCH_GROUP = "build-result-with-details";

    private static final String PROJECT_BUILD_DETAILS_FETCH_GROUP = "project-build-details";

    private static final String PROJECT_ALL_DETAILS_FETCH_GROUP = "project-all-details";

    // ----------------------------------------------------------------------
    // Component Lifecycle
    // ----------------------------------------------------------------------

    public void initialize()
        throws InitializationException
    {
        pmf = jdoFactory.getPersistenceManagerFactory();
    }

    // ----------------------------------------------------------------------
    // ContinuumStore Implementation
    // ----------------------------------------------------------------------

    public ContinuumProject addProject( ContinuumProject project )
        throws ContinuumStoreException
    {
        return (ContinuumProject) addObject( project, PROJECT_DETAIL_FG );
    }

    public void removeProject( String projectId )
        throws ContinuumStoreException
    {
        PersistenceManager pm = pmf.getPersistenceManager();

        Transaction tx = pm.currentTransaction();

        try
        {
            tx.begin();

            Object id = pm.newObjectIdInstance( ContinuumProject.class, projectId );

            ContinuumProject project = (ContinuumProject) pm.getObjectById( id );

            // ----------------------------------------------------------------------
            // We need to remove this project reference from any schedule in the
            // system. So grab the list of schedules this project belongs to
            // then iterate through the collection of schedules removing the
            // reference to this project. This seems like a bit much but the
            // only thing that works.
            // ----------------------------------------------------------------------

            if ( project.getSchedules() != null && project.getSchedules().size() > 0 )
            {
                Set schedules = project.getSchedules();

                for ( Iterator i = schedules.iterator(); i.hasNext(); )
                {
                    ContinuumSchedule schedule = (ContinuumSchedule) i.next();

                    schedule.getProjects().remove( project );
                }
            }

            if ( project.getProjectGroup() != null )
            {
                ContinuumProjectGroup pg = project.getProjectGroup();

                pg.getProjects().remove( project );
            }

            pm.deletePersistent( project );

            tx.commit();
        }
        finally
        {
            rollback( tx );
        }
    }

    public ContinuumProject updateProject( ContinuumProject project )
        throws ContinuumStoreException
    {
        String checkoutErrorMessage = project.getCheckOutErrorMessage();

        String checkoutErrorException = project.getCheckOutErrorException();

        if ( checkoutErrorMessage != null && checkoutErrorMessage.length() > 255 )
        {
            project.setCheckOutErrorMessage( checkoutErrorMessage.substring( 0, 255 ) );
        }

        if ( checkoutErrorException != null && checkoutErrorException.length() > 255 )
        {
            project.setCheckOutErrorException( checkoutErrorException.substring( 0, 255 ) );
        }

        // ----------------------------------------------------------------------
        //
        // ----------------------------------------------------------------------

        PersistenceManager pm = pmf.getPersistenceManager();

        Transaction tx = pm.currentTransaction();

        try
        {
            tx.begin();

            // ----------------------------------------------------------------------
            // Work around for bug with M:N relationships. We must persist the list
            // of schedules or they don't get saved.
            // ----------------------------------------------------------------------

//            if ( project.getSchedules() != null && project.getSchedules().size() > 0 )
//            {
//                pm.attachCopyAll( project.getSchedules(), true );
//            }
//
//            if ( project.getProjectGroup() != null )
//            {
//                pm.attachCopy( project.getProjectGroup(), true );
//            }
//
//            if ( project.getScmResult() != null )
//            {
//                pm.attachCopy( project.getScmResult(), true );
//            }

            pm.attachCopy( project, true );

            tx.commit();

            return (ContinuumProject) getDetailedObject( ContinuumProject.class, project.getId(), PROJECT_DETAIL_FG );
        }
        finally
        {
            rollback( tx );
        }
    }

    public Collection getAllProjects()
        throws ContinuumStoreException
    {
        PersistenceManager pm = pmf.getPersistenceManager();

        Transaction tx = pm.currentTransaction();

        try
        {
            tx.begin();

            Extent extent = pm.getExtent( ContinuumProject.class, true );

            Query query = pm.newQuery( extent );

            query.setOrdering( "name ascending" );

            Collection result = (Collection) query.execute();

            result = pm.detachCopyAll( result );

            for ( Iterator it = result.iterator(); it.hasNext(); )
            {
                setProjectState( (ContinuumProject) it.next() );
            }

            tx.commit();

            return result;
        }
        finally
        {
            rollback( tx );
        }
    }

    public ContinuumProject getProjectByName( String name )
        throws ContinuumStoreException
    {
        PersistenceManager pm = pmf.getPersistenceManager();

        Transaction tx = pm.currentTransaction();

        try
        {
            tx.begin();

            Extent extent = pm.getExtent( ContinuumProject.class, true );

            Query query = pm.newQuery( extent );

            query.declareImports( "import java.lang.String" );

            query.declareParameters( "String name" );

            query.setFilter( "this.name == name" );

            query.getFetchPlan().addGroup( PROJECT_DETAIL_FG );

            Collection result = (Collection) query.execute( name );

            if ( result.size() == 0 )
            {
                tx.commit();

                return null;
            }

            Object object = pm.detachCopy( result.iterator().next() );

            tx.commit();

            return (ContinuumProject) object;
        }
        finally
        {
            rollback( tx );
        }
    }

    public ContinuumProject getProject( String projectId )
        throws ContinuumStoreException
    {
        PersistenceManager pm = pmf.getPersistenceManager();

        Transaction tx = pm.currentTransaction();

        try
        {
            tx.begin();

            ContinuumProject project = getContinuumProject( pm, projectId, true );

            project = (ContinuumProject) pm.detachCopy( project );

            tx.commit();

            return project;
        }
        catch ( JDOObjectNotFoundException e )
        {
            throw new ContinuumObjectNotFoundException( ContinuumProject.class.getName(), projectId );
        }
        finally
        {
            rollback( tx );
        }
    }

    public ContinuumProject getProjectForBuild( String buildId )
        throws ContinuumStoreException
    {
        PersistenceManager pm = pmf.getPersistenceManager();

        Transaction tx = pm.currentTransaction();

        try
        {
            tx.begin();

            ContinuumBuild build = getContinuumBuild( pm, buildId );

            String projectId = build.getProject().getId();

            ContinuumProject project = getContinuumProject( pm, projectId, true );

            tx.commit();

            return project;
        }
        finally
        {
            rollback( tx );
        }
    }

    public ScmResult getScmResultForProject( String projectId )
        throws ContinuumStoreException
    {
        PersistenceManager pm = pmf.getPersistenceManager();

        Transaction tx = pm.currentTransaction();

        try
        {
            tx.begin();

            ContinuumProject project = getContinuumProject( pm, projectId, false );

            ScmResult scmResult = project.getScmResult();

            if ( scmResult == null )
            {
                tx.commit();

                return null;
            }

            scmResult = (ScmResult) pm.detachCopy( scmResult );

            tx.commit();

            return scmResult;
{'fraction_non_alphanumeric': 0.16635691570643868, 'fraction_numerical': 0.00239540742576302, 'mean_word_length': 1.6521358159912376, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1894943', 'n_tokens_mistral': 6044, 'n_tokens_neox': 5666, 'n_words': 1338}