~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/hibernate/HibernateImportObjectStore.java

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.dhis.importexport.hibernate;
 
2
 
 
3
/*
 
4
 * Copyright (c) 2004-2007, University of Oslo
 
5
 * All rights reserved.
 
6
 *
 
7
 * Redistribution and use in source and binary forms, with or without
 
8
 * modification, are permitted provided that the following conditions are met:
 
9
 * * Redistributions of source code must retain the above copyright notice, this
 
10
 *   list of conditions and the following disclaimer.
 
11
 * * Redistributions in binary form must reproduce the above copyright notice,
 
12
 *   this list of conditions and the following disclaimer in the documentation
 
13
 *   and/or other materials provided with the distribution.
 
14
 * * Neither the name of the HISP project nor the names of its contributors may
 
15
 *   be used to endorse or promote products derived from this software without
 
16
 *   specific prior written permission.
 
17
 *
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
21
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 
22
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 
25
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
28
 */
 
29
 
 
30
import java.util.Collection;
 
31
 
 
32
import org.hibernate.Criteria;
 
33
import org.hibernate.Query;
 
34
import org.hibernate.Session;
 
35
import org.hibernate.criterion.Restrictions;
 
36
import org.hisp.dhis.hibernate.HibernateSessionManager;
 
37
import org.hisp.dhis.importexport.GroupMemberType;
 
38
import org.hisp.dhis.importexport.ImportObject;
 
39
import org.hisp.dhis.importexport.ImportObjectStatus;
 
40
import org.hisp.dhis.importexport.ImportObjectStore;
 
41
 
 
42
/**
 
43
 * @author Lars Helge Overland
 
44
 * @version $Id: HibernateImportObjectStore.java 5793 2008-10-02 14:14:00Z larshelg $
 
45
 */
 
46
public class HibernateImportObjectStore
 
47
    implements ImportObjectStore
 
48
{
 
49
    // ----------------------------------------------------------------------
 
50
    // Dependencies
 
51
    // ----------------------------------------------------------------------
 
52
 
 
53
    private HibernateSessionManager sessionManager;
 
54
 
 
55
    public void setSessionManager( HibernateSessionManager sessionManager )
 
56
    {
 
57
        this.sessionManager = sessionManager;
 
58
    }
 
59
    
 
60
    // ----------------------------------------------------------------------
 
61
    // ImportObjectStore implementation
 
62
    // ----------------------------------------------------------------------
 
63
 
 
64
    // ----------------------------------------------------------------------
 
65
    // ImportObject
 
66
    // ----------------------------------------------------------------------
 
67
 
 
68
    public int addImportObject( ImportObject importObject )
 
69
    {
 
70
        Session session = sessionManager.getCurrentSession();
 
71
        
 
72
        return (Integer) session.save( importObject );
 
73
    }
 
74
    
 
75
    public void updateImportObject( ImportObject importObject )
 
76
    {
 
77
        Session session = sessionManager.getCurrentSession();
 
78
        
 
79
        session.saveOrUpdate( importObject );
 
80
    }
 
81
    
 
82
    public ImportObject getImportObject( int id )
 
83
    {
 
84
        Session session = sessionManager.getCurrentSession();
 
85
                
 
86
        return (ImportObject) session.get( ImportObject.class, id );
 
87
    }
 
88
 
 
89
    @SuppressWarnings( "unchecked" )
 
90
    public Collection<ImportObject> getImportObjects( Class<?> clazz )
 
91
    {
 
92
        Session session = sessionManager.getCurrentSession();
 
93
        
 
94
        Criteria criteria = session.createCriteria( ImportObject.class );
 
95
        
 
96
        criteria.add( Restrictions.eq( "className", clazz.getName() ) );
 
97
        
 
98
        return criteria.list();
 
99
    }
 
100
    
 
101
    @SuppressWarnings( "unchecked" )
 
102
    public Collection<ImportObject> getImportObjects( ImportObjectStatus status, Class<?> clazz )
 
103
    {
 
104
        Session session = sessionManager.getCurrentSession();
 
105
        
 
106
        Criteria criteria = session.createCriteria( ImportObject.class );
 
107
        
 
108
        criteria.add( Restrictions.eq( "status", status ) );
 
109
        criteria.add( Restrictions.eq( "className", clazz.getName() ) );
 
110
        
 
111
        return criteria.list();
 
112
    }
 
113
    
 
114
    @SuppressWarnings( "unchecked" )
 
115
    public Collection<ImportObject> getImportObjects( GroupMemberType groupMemberType )
 
116
    {
 
117
        Session session = sessionManager.getCurrentSession();
 
118
        
 
119
        Criteria criteria = session.createCriteria( ImportObject.class );
 
120
        
 
121
        criteria.add( Restrictions.eq( "groupMemberType", groupMemberType ) );
 
122
        
 
123
        return criteria.list();
 
124
    }
 
125
    
 
126
    public void deleteImportObject( ImportObject importObject )
 
127
    {
 
128
        Session session = sessionManager.getCurrentSession();
 
129
        
 
130
        session.delete( importObject );
 
131
    }
 
132
    
 
133
    @SuppressWarnings( "unchecked" )
 
134
    public void deleteImportObjects( Class<?> clazz )
 
135
    {
 
136
        Session session = sessionManager.getCurrentSession();
 
137
        
 
138
        String hql = "from ImportObject where className = :className";
 
139
        
 
140
        Query query = session.createQuery( hql );
 
141
        
 
142
        query.setString( "className", clazz.getName() );
 
143
        
 
144
        Collection<ImportObject> importObjects = query.list();
 
145
        
 
146
        for ( ImportObject importObject : importObjects )
 
147
        {
 
148
            session.evict( importObject );
 
149
        }
 
150
        
 
151
        hql = "delete " + hql;
 
152
        
 
153
        query = session.createQuery( hql );
 
154
        
 
155
        query.setString( "className", clazz.getName() );
 
156
        
 
157
        query.executeUpdate();
 
158
    }
 
159
 
 
160
    @SuppressWarnings( "unchecked" )
 
161
    public void deleteImportObjects( GroupMemberType groupMemberType )
 
162
    {
 
163
        Session session = sessionManager.getCurrentSession();
 
164
 
 
165
        String hql = "from ImportObject where groupMemberType = :groupMemberType";
 
166
        
 
167
        Query query = session.createQuery( hql );
 
168
        
 
169
        query.setParameter( "groupMemberType", groupMemberType );
 
170
 
 
171
        Collection<ImportObject> importObjects = query.list();
 
172
        
 
173
        for ( ImportObject importObject : importObjects )
 
174
        {
 
175
            session.evict( importObject );
 
176
        }
 
177
        
 
178
        hql = "delete " + hql;
 
179
        
 
180
        query = session.createQuery( hql );
 
181
        
 
182
        query.setParameter( "groupMemberType", groupMemberType );
 
183
        
 
184
        query.executeUpdate();
 
185
    }
 
186
    
 
187
    @SuppressWarnings( "unchecked" )
 
188
    public void deleteImportObjects()
 
189
    {
 
190
        Session session = sessionManager.getCurrentSession();
 
191
 
 
192
        String hql = "from ImportObject";
 
193
        
 
194
        Collection<ImportObject> importObjects = session.createQuery( hql ).list();
 
195
 
 
196
        for ( ImportObject importObject : importObjects )
 
197
        {
 
198
            session.evict( importObject );
 
199
        }
 
200
        
 
201
        hql = "delete " + hql;
 
202
        
 
203
        session.createQuery( hql ).executeUpdate();
 
204
    }
 
205
}