Project

General

Profile

Download (3.05 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.persistence.dao.hibernate.common;
10

    
11
import java.util.ArrayList;
12
import java.util.List;
13
import java.util.UUID;
14

    
15
import org.apache.log4j.Logger;
16
import org.hibernate.Query;
17
import org.hibernate.Session;
18
import org.springframework.stereotype.Repository;
19

    
20
import eu.etaxonomy.cdm.model.media.Rights;
21
import eu.etaxonomy.cdm.persistence.dao.common.IRightsDao;
22
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
23

    
24
/**
25
 * @author k.luther
26
 * @since 15.02.2017
27
 *
28
 */
29
@Repository
30
public class RightsDaoImpl extends  LanguageStringBaseDaoImpl<Rights> implements IRightsDao  {
31
    private static final Logger logger = Logger.getLogger(RightsDaoImpl.class);
32
    /**
33
     * @param type
34
     */
35
    public RightsDaoImpl(Class rightsClass) {
36
        super(rightsClass);
37

    
38
    }
39

    
40
    public RightsDaoImpl(){
41
        super(Rights.class);
42
    }
43

    
44
    @Override
45
    public List<UuidAndTitleCache<Rights>> getUuidAndTitleCache(Integer limit, String pattern) {
46
        List<UuidAndTitleCache<Rights>> list = new ArrayList<UuidAndTitleCache<Rights>>();
47
        Session session = getSession();
48

    
49
        String queryString = "SELECT " +"r.uuid, r.id, r.text, agent.titleCache, type.titleCache FROM " + type.getSimpleName() + " AS r LEFT OUTER JOIN r.agent AS agent LEFT OUTER JOIN r.type as type";
50

    
51
        if (pattern != null){
52
            queryString += " WHERE ";
53
            queryString += " r.text LIKE :pattern";
54
            queryString += " OR agent.titleCache LIKE :pattern";
55
//            queryString += " OR type.titleCache LIKE :pattern";
56
        }
57

    
58

    
59

    
60
         Query query;
61
        //if (pattern != null){
62
            query = session.createQuery(queryString);
63
//      }else{
64
//          query = session.createQuery("SELECT " +"r.uuid, r.id, r.titleCache, ab.titleCache FROM " + type.getSimpleName() + " AS r LEFT OUTER JOIN r.authorship AS ab ");//"select uuid, titleCache from " + type.getSimpleName());
65
//      }
66

    
67
        if (limit != null){
68
            query.setMaxResults(limit);
69
        }
70
        if (pattern != null){
71
              pattern = pattern.replace("*", "%");
72
              pattern = pattern.replace("?", "_");
73
              pattern = pattern + "%";
74
              query.setParameter("pattern", pattern);
75
        }
76

    
77
        @SuppressWarnings("unchecked")
78
        List<Object[]> result = query.list();
79

    
80
        for(Object[] object : result){
81
            String rightsText = (String) object[2];
82

    
83
            if(rightsText == null){
84
                rightsText = "no text";
85
            }
86
            String agentTitle = (String) object[3];
87

    
88
            String typeLabel = (String) object[4];
89
            rightsText = rightsText + " - " + agentTitle + " - " + typeLabel;
90

    
91
            list.add(new UuidAndTitleCache<Rights>(Rights.class, (UUID) object[0],(Integer)object[1], rightsText));
92

    
93
        }
94

    
95
        return list;
96
    }
97

    
98

    
99
}
(15-15/18)