1 package com.sri.emo.wizard.expressoimpl;
2
3 import com.jcorporate.expresso.core.controller.NonHandleableException;
4 import com.jcorporate.expresso.core.db.DBException;
5 import com.jcorporate.expresso.core.db.exception.DBRecordNotFoundException;
6 import com.jcorporate.expresso.core.registry.RequestRegistry;
7 import com.sri.common.dbobj.ObjectNotFoundException;
8 import com.sri.common.dbobj.RepositoryException;
9 import com.sri.emo.dbobj.WizDefinition;
10 import com.sri.emo.wizard.WizDefinitionRepository;
11
12 import java.util.List;
13
14 /***
15 * Expresso-aware wizard repository.
16 *
17 * @author Michael Rimov
18 * @version 1.0
19 */
20 public class SelectionWizDefinitionRepository implements WizDefinitionRepository {
21
22 /***
23 * Default Cosntructor.
24 */
25 public SelectionWizDefinitionRepository() {
26 }
27
28 /***
29 * Find a wizard definition by id.
30 *
31 * @param key int the wizard id.
32 * @return WizDefinition
33 * @throws DBRecordNotFoundException if the key doesn't list anything.
34 * @throws NonHandleableException on underlying database error.
35 */
36 public WizDefinition findById(final int key) throws ObjectNotFoundException, RepositoryException {
37
38 try {
39 WizDefinition wizdef = new WizDefinition();
40 wizdef.setRequestingUser(RequestRegistry.getUser());
41 wizdef.setId(key);
42 wizdef.retrieve();
43 return wizdef;
44 } catch (DBRecordNotFoundException ex) {
45 throw new ObjectNotFoundException("Wizard Definition with key of: " + key + " was not found", ex);
46 } catch (DBException ex) {
47 throw new NonHandleableException("Error querying underlying database", ex);
48 }
49 }
50
51 /***
52 * Lists all wizards defined by the system.
53 *
54 * @param sortOrder String the field to sort by or DEFAULT_SORT_ORDER if
55 * no specific order is desired.
56 * @return WizDefinition the wizard definition.
57 * @throws NonHandleableException upon underlying query error.
58 */
59 public List listAll(final String sortOrder) throws RepositoryException {
60 try {
61 WizDefinition wizdef = new WizDefinition();
62 wizdef.setRequestingUser(RequestRegistry.getUser());
63
64 List result;
65 if (sortOrder == DEFAULT_SORT_ORDER) {
66 result = wizdef.searchAndRetrieveList();
67 } else {
68 result = wizdef.searchAndRetrieveList(sortOrder);
69 }
70
71 if (result.size() == 0) {
72 return NO_RESULTS;
73 } else {
74 return result;
75 }
76 } catch (DBException ex) {
77 throw new RepositoryException("Error querying underlying database", ex);
78 }
79 }
80
81 /***
82 * Returns true if the current requester can add.
83 *
84 * @return boolean true.
85 * @throws NonHandleableException
86 */
87 public boolean canRequesterAdd() throws RepositoryException {
88 try {
89 WizDefinition wizdef = new WizDefinition();
90 wizdef.setRequestingUser(RequestRegistry.getUser());
91 return wizdef.canRequesterAdd();
92 } catch (DBException ex) {
93 throw new RepositoryException("Cannot determine if requester can add records.", ex);
94 }
95 }
96 }