made label text changeable
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / store / SearchManager.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.store;
12
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.List;
16
17 import org.eclipse.jface.dialogs.MessageDialog;
18 import org.eclipse.swt.widgets.Display;
19
20 import eu.etaxonomy.cdm.api.service.IAgentService;
21 import eu.etaxonomy.cdm.api.service.IGroupService;
22 import eu.etaxonomy.cdm.api.service.INameService;
23 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
24 import eu.etaxonomy.cdm.api.service.IReferenceService;
25 import eu.etaxonomy.cdm.api.service.ITaxonService;
26 import eu.etaxonomy.cdm.api.service.IUserService;
27 import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
28 import eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator;
29 import eu.etaxonomy.cdm.model.agent.AgentBase;
30 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
31 import eu.etaxonomy.cdm.model.common.Group;
32 import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
33 import eu.etaxonomy.cdm.model.common.RelationshipBase;
34 import eu.etaxonomy.cdm.model.common.User;
35 import eu.etaxonomy.cdm.model.common.UuidAndTitleCache;
36 import eu.etaxonomy.cdm.model.name.NameRelationship;
37 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
38 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
39 import eu.etaxonomy.cdm.model.reference.Reference;
40 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
41
42 /**
43 * @author n.hoffmann
44 * @created Dec 8, 2010
45 * @version 1.0
46 */
47 public class SearchManager {
48
49 public static final List NO_RESULTS = Arrays.asList(new Object[]{});
50
51 public static final String WILDCARD = "*";
52
53 public static int NO_COUNT = -1;
54
55 // TODO make this configurable via preferences
56 private static final int MAX_RESULTS_BEFORE_WARNING = 500;
57
58 /**
59 * <p>findNames</p>
60 *
61 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
62 * @return a {@link java.util.List} object.
63 */
64 public List<TaxonNameBase> findNames(IIdentifiableEntityServiceConfigurator configurator){
65 if(checkLargeResult(CdmStore.getService(INameService.class).countByTitle(configurator))){
66 return CdmStore.getService(INameService.class).findByTitle(configurator).getRecords();
67 }
68 return NO_RESULTS;
69 }
70
71 public List<NameRelationship> findNameRelationships(
72 IIdentifiableEntityServiceConfigurator configurator) {
73 if(true){
74 return NO_RESULTS;
75 }
76
77 List<NameRelationship> relationships = new ArrayList<NameRelationship>();
78 List<RelationshipBase> all = CdmStore.getService(INameService.class).getAllRelationships(0, 0);
79
80 for (RelationshipBase relationship : all){
81 if(relationship instanceof NameRelationship){
82 relationships.add((NameRelationship) relationship);
83 }
84 }
85
86
87 return relationships;
88
89 }
90
91 /**
92 * <p>findTaxaAndNames</p>
93 *
94 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator} object.
95 * @return a {@link java.util.List} object.
96 */
97 public List<UuidAndTitleCache<TaxonBase>> findTaxaAndNames(ITaxonServiceConfigurator configurator){
98 return CdmStore.getService(ITaxonService.class).findTaxaAndNamesForEditor(configurator);
99 }
100
101
102 /**
103 * <p>findReferences</p>
104 *
105 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
106 * @return a {@link java.util.List} object.
107 */
108 public List<Reference> findReferences(IIdentifiableEntityServiceConfigurator configurator){
109 if(checkLargeResult(CdmStore.getService(IReferenceService.class).countByTitle(configurator))){
110 return CdmStore.getService(IReferenceService.class).findByTitle(configurator).getRecords();
111 }
112 return NO_RESULTS;
113 }
114
115
116 /**
117 * <p>findAgents</p>
118 *
119 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
120 * @return a {@link java.util.List} object.
121 */
122 public List<AgentBase> findAgents(IIdentifiableEntityServiceConfigurator configurator){
123 if(checkLargeResult(CdmStore.getService(IAgentService.class).countByTitle(configurator))){
124 return CdmStore.getService(IAgentService.class).findByTitle(configurator).getRecords();
125 }
126 return NO_RESULTS;
127 }
128
129 /**
130 * <p>findTeamOrPersons</p>
131 *
132 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
133 * @return a {@link java.util.List} object.
134 */
135 public List<AgentBase> findTeamOrPersons(IIdentifiableEntityServiceConfigurator configurator){
136 configurator.setClazz(TeamOrPersonBase.class);
137 return findAgents(configurator);
138 }
139
140
141 /**
142 * <p>findOccurrences</p>
143 *
144 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
145 * @return a {@link java.util.List} object.
146 */
147 public List<SpecimenOrObservationBase> findOccurrences(IIdentifiableEntityServiceConfigurator configurator){
148 if(checkLargeResult(CdmStore.getService(IOccurrenceService.class).countByTitle(configurator))){
149 return CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords();
150 }
151 return NO_RESULTS;
152 }
153
154
155 public List<User> findUsers(IIdentifiableEntityServiceConfigurator configurator){
156 String userNameSearchString = sqlizeTitleSearchString(configurator);
157 // TODO why are users not identifiable entities?
158 return CdmStore.getService(IUserService.class).listByUsername(userNameSearchString, null, null, null, null, null, null);
159 }
160
161
162 public List<Group> findGroups(IIdentifiableEntityServiceConfigurator configurator){
163 String groupNameSearchString = sqlizeTitleSearchString(configurator);
164 // TODO why are groups not identifiable entities?
165 return CdmStore.getService(IGroupService.class).listByName(groupNameSearchString, null, null, null, null, null, null);
166 }
167
168
169 /**
170 * @param count
171 * @return
172 */
173 private boolean checkLargeResult(int count) {
174 if(count > MAX_RESULTS_BEFORE_WARNING){
175 return MessageDialog.openConfirm(Display.getDefault().getActiveShell(), "Large result expected",
176 String.format("The current search will return %s objects. This will " +
177 "take a long time and/or might render the editor unusable. Please consider refining your search.", count));
178 }else{
179 return true;
180 }
181 }
182
183 /**
184 *
185 * @param configurator
186 * @return
187 */
188 private String sqlizeTitleSearchString(IIdentifiableEntityServiceConfigurator configurator){
189 return configurator.getTitleSearchString().replace(WILDCARD, "%");
190 }
191
192
193 }