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

« back to all changes in this revision

Viewing changes to dhis-2/dhis-services/dhis-service-gis/src/main/java/org/hisp/dhis/gis/hibernate/HibernateFeatureStore.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.gis.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.Session;
 
34
import org.hibernate.criterion.Restrictions;
 
35
import org.hisp.dhis.gis.Feature;
 
36
import org.hisp.dhis.gis.FeatureStore;
 
37
import org.hisp.dhis.gis.MapFile;
 
38
import org.hisp.dhis.hibernate.HibernateSessionManager;
 
39
import org.hisp.dhis.organisationunit.OrganisationUnit;
 
40
 
 
41
/**
 
42
 * @author Tran Thanh Tri
 
43
 * @version $Id: Feature.java 28-01-2008 16:06:00 $
 
44
 */
 
45
public class HibernateFeatureStore
 
46
    implements FeatureStore
 
47
{
 
48
    // -------------------------------------------------------------------------
 
49
    // Dependencies
 
50
    // -------------------------------------------------------------------------
 
51
 
 
52
    private HibernateSessionManager sessionManager;
 
53
 
 
54
    public HibernateSessionManager getSessionManager()
 
55
    {
 
56
        return sessionManager;
 
57
    }
 
58
 
 
59
    // -------------------------------------------------------------------------
 
60
    // FeatureStore implementation
 
61
    // -------------------------------------------------------------------------
 
62
 
 
63
    public void setSessionManager( HibernateSessionManager sessionManager )
 
64
    {
 
65
        this.sessionManager = sessionManager;
 
66
    }
 
67
 
 
68
    public void add( Feature feature )
 
69
    {
 
70
        Session session = sessionManager.getCurrentSession();
 
71
        session.save( feature );
 
72
    }
 
73
 
 
74
    public void delete( Feature feature )
 
75
    {
 
76
        Session session = sessionManager.getCurrentSession();
 
77
        session.delete( feature );
 
78
    }
 
79
 
 
80
    public Feature get( int id )
 
81
    {
 
82
        Session session = sessionManager.getCurrentSession();
 
83
 
 
84
        return (Feature) session.get( Feature.class, new Integer( id ) );
 
85
    }
 
86
 
 
87
    public Feature get( String featureCode )
 
88
    {
 
89
        Session session = sessionManager.getCurrentSession();
 
90
        Criteria criteria = session.createCriteria( Feature.class );
 
91
        criteria.add( Restrictions.eq( "featureCode", featureCode ) );
 
92
        return (Feature) criteria.uniqueResult();
 
93
    }
 
94
 
 
95
    public Feature get( OrganisationUnit organisationUnit )
 
96
    {
 
97
        Session session = sessionManager.getCurrentSession();
 
98
 
 
99
        return (Feature) session.createQuery( "from Feature as f where f.organisationUnit = ?" ).setEntity( 0,
 
100
            organisationUnit ).uniqueResult();
 
101
    }
 
102
 
 
103
    @SuppressWarnings( "unchecked" )
 
104
    public Collection<Feature> getAll()
 
105
    {
 
106
        Session session = sessionManager.getCurrentSession();
 
107
 
 
108
        return session.createCriteria( Feature.class ).list();
 
109
    }
 
110
 
 
111
    public void update( Feature feature )
 
112
    {
 
113
        Session session = sessionManager.getCurrentSession();
 
114
 
 
115
        session.update( feature );
 
116
    }
 
117
 
 
118
    public void delete( String featureCode )
 
119
    {
 
120
        Session session = sessionManager.getCurrentSession();
 
121
 
 
122
        session.createQuery( "delete Feature as f where f.featureCode = ?" ).setEntity( 0, featureCode );
 
123
    }
 
124
 
 
125
    public void addMapFile( MapFile arg0 )
 
126
    {
 
127
        Session session = sessionManager.getCurrentSession();
 
128
 
 
129
        session.save( arg0 );
 
130
    }
 
131
 
 
132
    public void deleteMapFile( MapFile arg0 )
 
133
    {
 
134
        Session session = sessionManager.getCurrentSession();
 
135
 
 
136
        session.delete( arg0 );
 
137
    }
 
138
 
 
139
    @SuppressWarnings( "unchecked" )
 
140
    public Collection<MapFile> getAllMapFile()
 
141
    {
 
142
        Session session = sessionManager.getCurrentSession();
 
143
 
 
144
        return session.createCriteria( MapFile.class ).list();
 
145
    }
 
146
 
 
147
    public MapFile getMapFile( int id )
 
148
    {
 
149
        Session session = sessionManager.getCurrentSession();
 
150
 
 
151
        return (MapFile) session.get( MapFile.class, new Integer( id ) );
 
152
    }
 
153
 
 
154
    public MapFile getMapFile( OrganisationUnit organisationUnit )
 
155
    {
 
156
        Session session = sessionManager.getCurrentSession();
 
157
 
 
158
        return (MapFile) session.createQuery( "from MapFile as f where f.organisationUnit = ?" ).setEntity( 0,
 
159
            organisationUnit ).uniqueResult();
 
160
    }
 
161
 
 
162
    public void updateMapFile( MapFile arg0 )
 
163
    {
 
164
        Session session = sessionManager.getCurrentSession();
 
165
 
 
166
        session.update( arg0 );
 
167
    }
 
168
}