/*
 * The Gemma project
 * 
 * Copyright (c) 2006 University of British Columbia
 * 
 * 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.
 *
 */
package ubic.gemma.persistence;

import org.hibernate.FlushMode;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ubic.gemma.model.common.Auditable;
import ubic.gemma.model.common.auditAndSecurity.CurationDetailsDao;
import ubic.gemma.model.common.auditAndSecurity.curation.Curatable;
import ubic.gemma.model.common.auditAndSecurity.curation.CurationDetails;

/**
 * A service that knows how to persist Gemma-domain objects. Associations are checked and persisted in turn if needed.
 * Where appropriate, objects are only created anew if they don't already exist in the database, according to rules
 * documented elsewhere.
 *
 * @author pavlidis
 * @author keshav
 */
@Service
public class PersisterHelper extends RelationshipPersister {

    private final CurationDetailsDao curationDetailsDao;

    @Autowired
    public PersisterHelper( SessionFactory sessionFactory, CurationDetailsDao curationDetailsDao ) {
        super.setSessionFactory( sessionFactory );
        this.curationDetailsDao = curationDetailsDao;
    }

    @Override
    @Transactional
    public Object persist( Object entity ) {
        try {

            this.getSessionFactory().getCurrentSession().setFlushMode( FlushMode.COMMIT );

            if ( entity instanceof Curatable ) {
                this.initializeCurationDetails( ( Curatable ) entity );
            }

            return super.persist( entity );
        } finally {
            this.getSession().setFlushMode( FlushMode.AUTO );
        }
    }

    private void initializeCurationDetails( Curatable curatable ) {
        if ( curatable.getCurationDetails() == null ) {
            CurationDetails cd = this.curationDetailsDao.create();
            curatable.setCurationDetails( cd );
        }
    }

}

@@ -957,24 +957,84 @@
 .Auditable;%0A
+import ubic.gemma.model.common.auditAndSecurity.AuditTrail;%0A
 import ubic.
@@ -1608,24 +1608,39 @@
 ersister %7B%0A%0A
+    @Autowired%0A
     private 
@@ -1643,14 +1643,8 @@
 ate 
-final 
 Cura
@@ -1755,47 +1755,8 @@
 tory
-, CurationDetailsDao curationDetailsDao
  ) %7B
@@ -1811,62 +1811,8 @@
  );%0A
-        this.curationDetailsDao = curationDetailsDao;%0A
     
@@ -1999,16 +1999,381 @@
 MIT );%0A%0A
+            if ( entity instanceof Auditable ) %7B%0A                Auditable auditable = ( Auditable ) entity;%0A%0A                if ( auditable.getAuditTrail() == null ) %7B%0A                    auditable.setAuditTrail( AuditTrail.Factory.newInstance() );%0A                %7D%0A%0A                auditable.setAuditTrail( persistAuditTrail( auditable.getAuditTrail() ) );%0A%0A    
         
@@ -2405,32 +2405,36 @@
 f Curatable ) %7B%0A
+    
                 
@@ -2481,32 +2481,50 @@
 ble ) entity );%0A
+                %7D%0A
             %7D%0A%0A 

Work on git Issue #1 - Fix curation details and audit trail persisting