1 | package com.ether.dao.simulation; |
---|
2 | |
---|
3 | import com.ether.PersistenceException; |
---|
4 | import com.ether.dao.SearchableAbstractHibernateMappingDAO; |
---|
5 | import com.ether.simulation.Simulation; |
---|
6 | import com.ether.simulation.SimulationFilter; |
---|
7 | import org.hibernate.criterion.DetachedCriteria; |
---|
8 | import org.hibernate.criterion.Order; |
---|
9 | import org.hibernate.criterion.Projections; |
---|
10 | import org.hibernate.criterion.Restrictions; |
---|
11 | import org.jetbrains.annotations.NotNull; |
---|
12 | import org.jetbrains.annotations.Nullable; |
---|
13 | import org.springframework.util.StringUtils; |
---|
14 | |
---|
15 | import java.util.ArrayList; |
---|
16 | import java.util.List; |
---|
17 | |
---|
18 | /** |
---|
19 | * @author vmipsl |
---|
20 | * @date 13 juillet 2012 |
---|
21 | */ |
---|
22 | public class SimulationDAOImpl |
---|
23 | extends SearchableAbstractHibernateMappingDAO<Simulation, Integer, SimulationFilter> |
---|
24 | implements SimulationDAO |
---|
25 | { |
---|
26 | protected SimulationDAOImpl() |
---|
27 | { |
---|
28 | super( Simulation.class, Integer.class ); |
---|
29 | } |
---|
30 | |
---|
31 | @Nullable |
---|
32 | public Simulation getSimulationByLocation( @NotNull final String location ) |
---|
33 | throws PersistenceException |
---|
34 | { |
---|
35 | final DetachedCriteria criteria = DetachedCriteria.forClass( Simulation.class ) |
---|
36 | .add( Restrictions.eq( "location", location ) ); |
---|
37 | return selectByCriteria( Simulation.class, criteria ); |
---|
38 | } |
---|
39 | |
---|
40 | @Nullable |
---|
41 | public List<List<String>> getAllModels() |
---|
42 | throws PersistenceException |
---|
43 | { |
---|
44 | final DetachedCriteria criteriaTitles = createCriteriaForModels( "Title", true ); |
---|
45 | final List<Object[]> listTitles = selectAllByCriteria( Object[].class, criteriaTitles ); |
---|
46 | |
---|
47 | final DetachedCriteria criteriaVersions = createCriteriaForModels( "Chimere_version", true ); |
---|
48 | final List<Object[]> listVersions = selectAllByCriteria( Object[].class, criteriaVersions ); |
---|
49 | |
---|
50 | final DetachedCriteria criteriaLevels = createCriteriaForModels( "bottom_top", false ); |
---|
51 | final List<Object[]> listLevels = selectAllByCriteria( Object[].class, criteriaLevels ); |
---|
52 | |
---|
53 | final List<List<String>> modelsList = new ArrayList<List<String>>(); |
---|
54 | int i = 0; |
---|
55 | for( final Object[] title : listTitles ) |
---|
56 | { |
---|
57 | final String version = (String) listVersions.get( i )[1]; |
---|
58 | final String level = (String) listLevels.get( i )[1]; |
---|
59 | final List<String> values = new ArrayList<String>( 3 ); |
---|
60 | values.add( title[0].toString() ); |
---|
61 | values.add( (String) title[1] ); |
---|
62 | values.add( version ); |
---|
63 | values.add( (String) title[2] ); |
---|
64 | values.add( level ); |
---|
65 | i++; |
---|
66 | modelsList.add( values ); |
---|
67 | } |
---|
68 | |
---|
69 | return modelsList; |
---|
70 | } |
---|
71 | |
---|
72 | @Nullable |
---|
73 | public String getLocationByModelId( @NotNull final Integer modelId ) |
---|
74 | throws PersistenceException |
---|
75 | { |
---|
76 | final DetachedCriteria criteria = DetachedCriteria.forClass( Simulation.class ) |
---|
77 | .add( Restrictions.idEq( modelId ) ) |
---|
78 | .setProjection( Projections.property( "location" ) ); |
---|
79 | |
---|
80 | return selectByCriteria( String.class, criteria ); |
---|
81 | } |
---|
82 | |
---|
83 | @Override |
---|
84 | protected DetachedCriteria searchCriteria( @NotNull final SimulationFilter filter, final boolean isForCount ) |
---|
85 | { |
---|
86 | final DetachedCriteria criteria = DetachedCriteria.forClass( Simulation.class ); |
---|
87 | |
---|
88 | if( StringUtils.hasText( filter.getSearchText() ) ) |
---|
89 | criteria.add( Restrictions.or( escapedLike( "location", filter.getSearchText() ), escapedLike( "xmlnsHeader", filter.getSearchText() ) ) ); |
---|
90 | |
---|
91 | if( !isForCount ) |
---|
92 | criteria.addOrder( Order.asc( filter.getSort() ) ); |
---|
93 | return criteria; |
---|
94 | } |
---|
95 | |
---|
96 | @NotNull |
---|
97 | private DetachedCriteria createCriteriaForModels( @NotNull final String attributeName, final boolean isAttribute ) |
---|
98 | { |
---|
99 | final DetachedCriteria criteria = DetachedCriteria.forClass( Simulation.class, "simulation" ); |
---|
100 | if( isAttribute ) |
---|
101 | criteria.createCriteria( "attribute", "attribute" ) |
---|
102 | .add( Restrictions.eq( "attribute.name", attributeName ) ) |
---|
103 | .setProjection( Projections.projectionList() |
---|
104 | .add( Projections.property( "simulation.id" ) ) |
---|
105 | .add( Projections.property( "attribute.value" ) ) |
---|
106 | .add( Projections.property( "simulation.dates" ) ) |
---|
107 | ); |
---|
108 | else |
---|
109 | criteria.createCriteria( "dimension", "dimension" ) |
---|
110 | .add( Restrictions.eq( "dimension.name", attributeName ) ) |
---|
111 | .setProjection( Projections.projectionList() |
---|
112 | .add( Projections.property( "simulation.id" ) ) |
---|
113 | .add( Projections.property( "dimension.length" ) ) |
---|
114 | .add( Projections.property( "simulation.dates" ) ) |
---|
115 | ); |
---|
116 | |
---|
117 | return criteria.addOrder( Order.asc( "simulation.id" ) ); |
---|
118 | } |
---|
119 | } |
---|