comparison src/main/java/edu/harvard/iq/dataverse/DatasetFieldServiceBean.java @ 10:a50cf11e5178

Rewrite LGDataverse completely upgrading to dataverse4.0
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Tue, 08 Sep 2015 17:00:21 +0200
parents
children
comparison
equal deleted inserted replaced
9:5926d6419569 10:a50cf11e5178
1 /*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6 package edu.harvard.iq.dataverse;
7
8 import java.util.List;
9 import javax.ejb.Stateless;
10 import javax.inject.Named;
11 import javax.persistence.EntityManager;
12 import javax.persistence.NoResultException;
13 import javax.persistence.NonUniqueResultException;
14 import javax.persistence.PersistenceContext;
15 import javax.persistence.Query;
16 import javax.persistence.TypedQuery;
17
18 /**
19 *
20 * @author xyang
21 */
22 @Stateless
23 @Named
24 public class DatasetFieldServiceBean implements java.io.Serializable {
25
26 @PersistenceContext(unitName = "VDCNet-ejbPU")
27 private EntityManager em;
28
29 private static final String NAME_QUERY = "SELECT dsfType from DatasetFieldType dsfType where dsfType.name= :fieldName";
30
31 public List<DatasetFieldType> findAllAdvancedSearchFieldTypes() {
32 return em.createQuery("select object(o) from DatasetFieldType as o where o.advancedSearchFieldType = true and o.title != '' order by o.id").getResultList();
33 }
34
35 public List<DatasetFieldType> findAllFacetableFieldTypes() {
36 return em.createNamedQuery("DatasetFieldType.findAllFacetable", DatasetFieldType.class)
37 .getResultList();
38 }
39
40 public List<DatasetFieldType> findFacetableFieldTypesByMetadataBlock(Long metadataBlockId) {
41 return em.createNamedQuery("DatasetFieldType.findFacetableByMetadaBlock", DatasetFieldType.class)
42 .setParameter("metadataBlockId", metadataBlockId)
43 .getResultList();
44 }
45
46 public List<DatasetFieldType> findAllRequiredFields() {
47 return em.createQuery("select object(o) from DatasetFieldType as o where o.required = true order by o.id").getResultList();
48 }
49
50 public List<DatasetFieldType> findAllOrderedById() {
51 return em.createQuery("select object(o) from DatasetFieldType as o order by o.id").getResultList();
52 }
53
54 public List<DatasetFieldType> findAllOrderedByName() {
55 return em.createQuery("select object(o) from DatasetFieldType as o order by o.name").getResultList();
56 }
57
58 public DatasetFieldType find(Object pk) {
59 return (DatasetFieldType) em.find(DatasetFieldType.class, pk);
60 }
61
62 public DatasetFieldType findByName(String name) {
63 try {
64 return (DatasetFieldType) em.createQuery(NAME_QUERY).setParameter("fieldName", name).getSingleResult();
65 } catch (NoResultException e) {
66 return null;
67 }
68
69 }
70
71 /**
72 * Gets the dataset field type, or returns {@code null}. Does not throw
73 * exceptions.
74 *
75 * @param name the name do the field type
76 * @return the field type, or {@code null}
77 * @see #findByName(java.lang.String)
78 */
79 public DatasetFieldType findByNameOpt(String name) {
80 try {
81 return em.createNamedQuery("DatasetFieldType.findByName", DatasetFieldType.class)
82 .setParameter("name", name)
83 .getSingleResult();
84 } catch (NoResultException nre) {
85 return null;
86 }
87 }
88
89 /*
90 * Similar method for looking up foreign metadata field mappings, for metadata
91 * imports. for these the uniquness of names isn't guaranteed (i.e., there
92 * can be a field "author" in many different formats that we want to support),
93 * so these have to be looked up by both the field name and the name of the
94 * foreign format.
95 */
96 public ForeignMetadataFieldMapping findFieldMapping(String formatName, String pathName) {
97 try {
98 return em.createNamedQuery("ForeignMetadataFieldMapping.findByPath", ForeignMetadataFieldMapping.class)
99 .setParameter("formatName", formatName)
100 .setParameter("xPath", pathName)
101 .getSingleResult();
102 } catch (NoResultException nre) {
103 return null;
104 }
105
106 // TODO: cache looked up results.
107 }
108
109 public ControlledVocabularyValue findControlledVocabularyValue(Object pk) {
110 return (ControlledVocabularyValue) em.find(ControlledVocabularyValue.class, pk);
111 }
112
113 /**
114 * @param dsft The DatasetFieldType in which to look up a
115 * ControlledVocabularyValue.
116 * @param strValue String value that may exist in a controlled vocabulary of
117 * the provided DatasetFieldType.
118 * @param lenient should we accept alternate spellings for value from mapping table
119 *
120 * @return The ControlledVocabularyValue found or null.
121 */
122 public ControlledVocabularyValue findControlledVocabularyValueByDatasetFieldTypeAndStrValue(DatasetFieldType dsft, String strValue, boolean lenient) {
123 TypedQuery<ControlledVocabularyValue> typedQuery = em.createQuery("SELECT OBJECT(o) FROM ControlledVocabularyValue AS o WHERE o.strValue = :strvalue AND o.datasetFieldType = :dsft", ControlledVocabularyValue.class);
124 typedQuery.setParameter("strvalue", strValue);
125 typedQuery.setParameter("dsft", dsft);
126 try {
127 ControlledVocabularyValue cvv = typedQuery.getSingleResult();
128 return cvv;
129 } catch (NoResultException | NonUniqueResultException ex) {
130 if (lenient) {
131 // if the value isn't found, check in the list of alternate values for this datasetFieldType
132 TypedQuery<ControlledVocabAlternate> alternateQuery = em.createQuery("SELECT OBJECT(o) FROM ControlledVocabAlternate as o WHERE o.strValue = :strvalue AND o.datasetFieldType = :dsft", ControlledVocabAlternate.class);
133 alternateQuery.setParameter("strvalue", strValue);
134 alternateQuery.setParameter("dsft", dsft);
135 try {
136 ControlledVocabAlternate alternateValue = alternateQuery.getSingleResult();
137 return alternateValue.getControlledVocabularyValue();
138 } catch (NoResultException | NonUniqueResultException ex2) {
139 return null;
140 }
141
142 } else {
143 return null;
144 }
145 }
146 }
147
148 // return singleton NA Controled Vocabulary Value
149 public ControlledVocabularyValue findNAControlledVocabularyValue() {
150 TypedQuery<ControlledVocabularyValue> typedQuery = em.createQuery("SELECT OBJECT(o) FROM ControlledVocabularyValue AS o WHERE o.datasetFieldType is null AND o.strValue = :strvalue", ControlledVocabularyValue.class);
151 typedQuery.setParameter("strvalue", DatasetField.NA_VALUE);
152 return typedQuery.getSingleResult();
153 }
154
155 public DatasetFieldType save(DatasetFieldType dsfType) {
156 return em.merge(dsfType);
157 }
158
159 public MetadataBlock save(MetadataBlock mdb) {
160 return em.merge(mdb);
161 }
162
163 public ControlledVocabularyValue save(ControlledVocabularyValue cvv) {
164 return em.merge(cvv);
165 }
166
167 public ControlledVocabAlternate save(ControlledVocabAlternate alt) {
168 return em.merge(alt);
169 }
170
171 }