|
23 | 23 |
|
24 | 24 | public class TopicsDb extends EntitiesDb<Topic, TopicAttributes> {
|
25 | 25 |
|
26 |
| - |
| 26 | + /** |
| 27 | + * This function will help to access to the database which only take the instance of "Topic" class |
| 28 | + * |
| 29 | + */ |
27 | 30 | @Override
|
28 |
| - protected LoadType<Topic> load() { |
| 31 | + protected LoadType<Topic> load() { |
29 | 32 | return ofy().load().type(Topic.class);
|
30 |
| - } |
31 |
| - |
32 |
| - public static final String ERROR_UPDATE_NON_EXISTENT_TOPIC = "Trying to update a Topic that doesn't exist: "; |
33 |
| - |
34 |
| - public void createTopics(Collection<TopicAttributes> topicsToAdd) throws InvalidParametersException { |
35 |
| - List<TopicAttributes> topicsToUpdate = createEntities(topicsToAdd); |
36 |
| - for (TopicAttributes topic : topicsToUpdate) { |
37 |
| - try { |
38 |
| - updateTopic(topic); |
39 |
| - } catch (EntityDoesNotExistException e) { |
40 |
| - // This situation is not tested as replicating such a situation is |
41 |
| - // difficult during testing |
42 |
| - Assumption.fail("Entity found be already existing and not existing simultaneously"); |
43 |
| - } |
44 |
| - } |
45 |
| - } |
| 33 | + } |
46 | 34 |
|
47 |
| - /** |
48 |
| - * Preconditions: <br> |
49 |
| - * * All parameters are non-null. |
50 |
| - * @return Null if not found. |
51 |
| - */ |
52 |
| - public TopicAttributes getTopic(String topicId) { |
53 |
| - Assumption.assertNotNull(Const.StatusCodes.DBLEVEL_NULL_INPUT, topicId); |
| 35 | + public static final String ERROR_UPDATE_NON_EXISTENT_TOPIC = "Trying to update a Topic that doesn't exist: "; |
54 | 36 |
|
55 |
| - return makeAttributesOrNull(getTopicEntity(topicId)); |
56 |
| - } |
57 | 37 |
|
58 |
| - public List<TopicAttributes> getTopics(List<String> topicIds) { |
59 |
| - Assumption.assertNotNull(Const.StatusCodes.DBLEVEL_NULL_INPUT, topicIds); |
60 | 38 |
|
61 |
| - return makeAttributes(getTopicEntities(topicIds)); |
62 |
| - } |
63 | 39 |
|
64 |
| - |
65 |
| - public void updateTopic(TopicAttributes topicToUpdate) throws InvalidParametersException, |
66 |
| - EntityDoesNotExistException { |
67 |
| - Assumption.assertNotNull(Const.StatusCodes.DBLEVEL_NULL_INPUT, topicToUpdate); |
| 40 | + /** |
| 41 | + * Preconditions: <br> |
| 42 | + * * All parameters are non-null. |
| 43 | + * @return Null if not found. |
| 44 | + */ |
68 | 45 |
|
69 |
| - topicToUpdate.sanitizeForSaving(); |
| 46 | + /** |
| 47 | + * Return a topic based of the topicId |
| 48 | + * @param topicId Name of the topic |
| 49 | + */ |
| 50 | + public TopicAttributes getTopic(String topicId) { |
| 51 | + Assumption.assertNotNull(Const.StatusCodes.DBLEVEL_NULL_INPUT, topicId); |
| 52 | + return makeAttributesOrNull(getTopicEntity(topicId)); |
| 53 | + } |
70 | 54 |
|
71 |
| - if (!topicToUpdate.isValid()) { |
72 |
| - throw new InvalidParametersException(topicToUpdate.getInvalidityInfo()); |
73 |
| - } |
| 55 | + /** |
| 56 | + * Return list of topics based of the list of topicIds |
| 57 | + * @param topicIds List of topics' name |
| 58 | + */ |
74 | 59 |
|
75 |
| - Topic topicEntityToUpdate = getTopicEntity(topicToUpdate.getName()); |
| 60 | + public List<TopicAttributes> getTopics(List<String> topicIds) { |
| 61 | + Assumption.assertNotNull(Const.StatusCodes.DBLEVEL_NULL_INPUT, topicIds); |
76 | 62 |
|
77 |
| - if (topicEntityToUpdate == null) { |
78 |
| - throw new EntityDoesNotExistException(ERROR_UPDATE_NON_EXISTENT_TOPIC); |
79 |
| - } |
| 63 | + return makeAttributes(getTopicEntities(topicIds)); |
| 64 | + } |
| 65 | + /** |
| 66 | + * Unused method for now. |
| 67 | + */ |
| 68 | + |
| 69 | + public void updateTopic(TopicAttributes topicToUpdate) throws InvalidParametersException, |
| 70 | + EntityDoesNotExistException { |
| 71 | + Assumption.assertNotNull(Const.StatusCodes.DBLEVEL_NULL_INPUT, topicToUpdate); |
80 | 72 |
|
81 |
| - |
| 73 | + topicToUpdate.sanitizeForSaving(); |
82 | 74 |
|
83 |
| - saveEntity(topicEntityToUpdate, topicToUpdate); |
| 75 | + if (!topicToUpdate.isValid()) { |
| 76 | + throw new InvalidParametersException(topicToUpdate.getInvalidityInfo()); |
84 | 77 | }
|
| 78 | + Topic topicEntityToUpdate = getTopicEntity(topicToUpdate.getName()); |
85 | 79 |
|
86 |
| - /** |
87 |
| - * Note: This is a non-cascade delete.<br> |
88 |
| - * <br> Fails silently if there is no such object. |
89 |
| - * <br> Preconditions: |
90 |
| - * <br> * {@code topicId} is not null. |
91 |
| - */ |
92 |
| - |
| 80 | + if (topicEntityToUpdate == null) { |
| 81 | + throw new EntityDoesNotExistException(ERROR_UPDATE_NON_EXISTENT_TOPIC); |
| 82 | + } |
| 83 | + saveEntity(topicEntityToUpdate, topicToUpdate); |
| 84 | + } |
93 | 85 |
|
94 |
| - |
| 86 | + /** |
| 87 | + * Return a Topic which retrieved from database |
| 88 | + * @param attributes a topic attribute will be converted to Topic |
| 89 | + */ |
| 90 | + @Override |
| 91 | + protected Topic getEntity(TopicAttributes attributes) { |
| 92 | + return getTopicEntity(attributes.getName()); |
| 93 | + } |
95 | 94 |
|
96 |
| - @Override |
97 |
| - protected Topic getEntity(TopicAttributes attributes) { |
98 |
| - return getTopicEntity(attributes.getName()); |
99 |
| - } |
100 | 95 |
|
| 96 | + /** |
| 97 | + * Return a Topic which retrieved from database |
| 98 | + * @param topicId name of the Topic |
| 99 | + */ |
| 100 | + public Topic getTopicEntity(String topicId) { |
| 101 | + return load().id(topicId).now(); |
| 102 | + } |
101 | 103 |
|
102 |
| - public Topic getTopicEntity(String topicId) { |
103 |
| - return load().id(topicId).now(); |
| 104 | + private List<Topic> getTopicEntities(List<String> topicIds) { |
| 105 | + if (topicIds.isEmpty()) { |
| 106 | + return new ArrayList<>(); |
104 | 107 | }
|
105 | 108 |
|
106 |
| - private List<Topic> getTopicEntities(List<String> topicIds) { |
107 |
| - if (topicIds.isEmpty()) { |
108 |
| - return new ArrayList<>(); |
109 |
| - } |
| 109 | + return new ArrayList<>( |
| 110 | + load().ids(topicIds).values()); |
| 111 | + } |
110 | 112 |
|
111 |
| - return new ArrayList<>( |
112 |
| - load().ids(topicIds).values()); |
113 |
| - } |
| 113 | + /** |
| 114 | + * |
| 115 | + * Convert the object from Topic into TopicAttribute |
| 116 | + * @param entity name of the Topic |
| 117 | + * @return TopicAttributes |
| 118 | + */ |
| 119 | + @Override |
| 120 | + protected TopicAttributes makeAttributes(Topic entity) { |
| 121 | + Assumption.assertNotNull(Const.StatusCodes.DBLEVEL_NULL_INPUT, entity); |
| 122 | + |
| 123 | + return TopicAttributes.builder(entity.getName(), entity.getDesc()) |
| 124 | + .build(); |
| 125 | + } |
114 | 126 |
|
115 |
| - @Override |
116 |
| - protected TopicAttributes makeAttributes(Topic entity) { |
117 |
| - Assumption.assertNotNull(Const.StatusCodes.DBLEVEL_NULL_INPUT, entity); |
118 |
| - |
119 |
| - return TopicAttributes.builder(entity.getName(), entity.getDesc()) |
120 |
| - .build(); |
121 |
| - } |
122 |
| - |
123 |
| - @Override |
124 |
| - protected QueryKeys<Topic> getEntityQueryKeys(TopicAttributes attributes) { |
125 |
| - Key<Topic> keyToFind = Key.create(Topic.class, attributes.getName()); |
126 |
| - return load().filterKey(keyToFind).keys(); |
127 |
| - } |
128 |
| - |
129 |
| - public List<TopicAttributes> getAllTopics(){ |
130 |
| - return makeAttributes(load().list()); |
131 |
| - } |
| 127 | + @Override |
| 128 | + protected QueryKeys<Topic> getEntityQueryKeys(TopicAttributes attributes) { |
| 129 | + Key<Topic> keyToFind = Key.create(Topic.class, attributes.getName()); |
| 130 | + return load().filterKey(keyToFind).keys(); |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + /** |
| 135 | + * Retrieve all topics stored in the database |
| 136 | + * @return List<TopicAttributes> |
| 137 | + */ |
| 138 | + |
| 139 | + public List<TopicAttributes> getAllTopics(){ |
| 140 | + return makeAttributes(load().list()); |
| 141 | + } |
132 | 142 |
|
133 | 143 |
|
| 144 | + /** |
| 145 | + * Remove the topic in the databased based on the topicID |
| 146 | + * @param topicName it's an id of the object in the database |
| 147 | + */ |
134 | 148 | public void deleteTopic(String topicName) {
|
135 | 149 |
|
136 |
| - Assumption.assertNotNull(Const.StatusCodes.DBLEVEL_NULL_INPUT, topicName); |
| 150 | + Assumption.assertNotNull(Const.StatusCodes.DBLEVEL_NULL_INPUT, topicName); |
137 | 151 |
|
138 |
| - // only the courseId is important here, everything else are placeholders |
139 |
| - deleteEntity(TopicAttributes |
140 |
| - .builder(topicName, "Non-existent course") |
141 |
| - .build()); |
| 152 | + // only the courseId is important here, everything else are placeholders |
| 153 | + deleteEntity(TopicAttributes |
| 154 | + .builder(topicName, "Non-existent course") |
| 155 | + .build()); |
142 | 156 |
|
143 | 157 |
|
144 | 158 | }
|
|
0 commit comments