Skip to content

Commit bb428a6

Browse files
authored
Merge pull request #8 from RMIT-SEPT/development
Add comments and fix the description.
2 parents 2b16f4e + 8974416 commit bb428a6

File tree

11 files changed

+277
-397
lines changed

11 files changed

+277
-397
lines changed

src/main/java/teammates/common/datatransfer/attributes/TopicAttributes.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,23 @@
66
import java.util.ArrayList;
77
import java.util.List;
88

9-
import teammates.common.util.*;
9+
import teammates.common.util.Assumption;
10+
import teammates.common.util.FieldValidator;
11+
import teammates.common.util.JsonUtils;
12+
import teammates.common.util.SanitizationHelper;
1013
import teammates.storage.entity.Topic;
1114

1215
public class TopicAttributes extends EntityAttributes<Topic> {
1316

1417
public String name;
1518
public String desc;
16-
19+
public List<RepliesAttributes> replies;
1720
public TopicAttributes(String name, String desc) {
1821
this.name = SanitizationHelper.sanitizeTitle(name);
1922
this.desc = SanitizationHelper.sanitizeTitle(desc);
2023
}
2124

22-
25+
/*Builder is used as a constructor to initiate instance of TopicAttribute*/
2326
public static Builder builder(String name, String desc) {
2427
return new Builder(name, desc);
2528

src/main/java/teammates/logic/core/TopicsLogic.java

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class TopicsLogic {
1919

2020
private static TopicsLogic instance = new TopicsLogic();
2121

22-
/* Explanation: This class depends on TopicsDb class but no other *Db classes.
22+
/** Explanation: This class depends on TopicsDb class but no other *Db classes.
2323
* That is because reading/writing entities from/to the datastore is the
2424
* responsibility of the matching *Logic class.
2525
* However, this class can talk to other *Logic classes. That is because
@@ -37,10 +37,26 @@ private TopicsLogic() {
3737
// prevent initialization
3838
}
3939

40-
public TopicAttributes validateAndCreateTopicAttributes(String name, String desc)throws InvalidParametersException{
40+
41+
/**
42+
* This function is used to validate the parameters before initiate it to avoid errors.
43+
* @param name the topic name
44+
* @param desc the description of the topic
45+
* @return TopicAttribute instance
46+
*
47+
*/
48+
49+
public TopicAttributes validateAndCreateTopicAttributes(String name, String desc) throws InvalidParametersException{
4150
Assumption.assertNotNull("Non-null value expected", name);
4251
return TopicAttributes.builder(name,desc).build();
4352
}
53+
54+
/**
55+
* After valid the parameters by validateAndCreateTopicAttributes()
56+
* which will return an instance of TopicAttribute, it will store that object into the database
57+
* via createEntity() function.
58+
*
59+
*/
4460
public void createTopic(String name, String desc)
4561
throws InvalidParametersException, EntityAlreadyExistsException {
4662

@@ -51,7 +67,30 @@ public void createTopic(String name, String desc)
5167

5268
System.out.println("Topic entity has been created...");
5369
}
54-
70+
71+
/**
72+
* Return a list of all Topic stored in the database
73+
*
74+
*/
75+
public List<TopicAttributes> getAllTopic() {
76+
List<TopicAttributes> alltopics = topicsDb.getAllTopics();
77+
return alltopics;
78+
}
79+
80+
/**
81+
* Remove the topic with this method to make sure there is no conflict in future
82+
* All objects related to this topic will be cleaned.
83+
*
84+
*/
85+
86+
public void deleteTopicCascade(String topicName) {
87+
topicsDb.deleteTopic(topicName);
88+
}
89+
90+
// Get topic for testing
91+
public TopicAttributes getTopic(String topicName) {
92+
return topicsDb.getTopic(topicName);
93+
}
5594

5695
/**
5796
* Returns a list of {@link TopicAttributes} for all topics a given student is enrolled in.
@@ -138,17 +177,4 @@ public void createTopicForDiscussionBoard( String topicName, String topicDesc)
138177
}
139178

140179

141-
public List<TopicAttributes> getAllTopic() {
142-
List<TopicAttributes> alltopics = topicsDb.getAllTopics();
143-
return alltopics;
144-
}
145-
146-
public void deleteTopicCascade(String topicName) {
147-
topicsDb.deleteTopic(topicName);
148-
}
149-
150-
// Get topic for testing
151-
public TopicAttributes getTopic(String topicName) {
152-
return topicsDb.getTopic(topicName);
153-
}
154180
}

src/main/java/teammates/storage/api/TopicsDb.java

Lines changed: 102 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -23,122 +23,136 @@
2323

2424
public class TopicsDb extends EntitiesDb<Topic, TopicAttributes> {
2525

26-
26+
/**
27+
* This function will help to access to the database which only take the instance of "Topic" class
28+
*
29+
*/
2730
@Override
28-
protected LoadType<Topic> load() {
31+
protected LoadType<Topic> load() {
2932
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+
}
4634

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: ";
5436

55-
return makeAttributesOrNull(getTopicEntity(topicId));
56-
}
5737

58-
public List<TopicAttributes> getTopics(List<String> topicIds) {
59-
Assumption.assertNotNull(Const.StatusCodes.DBLEVEL_NULL_INPUT, topicIds);
6038

61-
return makeAttributes(getTopicEntities(topicIds));
62-
}
6339

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+
*/
6845

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+
}
7054

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+
*/
7459

75-
Topic topicEntityToUpdate = getTopicEntity(topicToUpdate.getName());
60+
public List<TopicAttributes> getTopics(List<String> topicIds) {
61+
Assumption.assertNotNull(Const.StatusCodes.DBLEVEL_NULL_INPUT, topicIds);
7662

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);
8072

81-
73+
topicToUpdate.sanitizeForSaving();
8274

83-
saveEntity(topicEntityToUpdate, topicToUpdate);
75+
if (!topicToUpdate.isValid()) {
76+
throw new InvalidParametersException(topicToUpdate.getInvalidityInfo());
8477
}
78+
Topic topicEntityToUpdate = getTopicEntity(topicToUpdate.getName());
8579

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+
}
9385

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+
}
9594

96-
@Override
97-
protected Topic getEntity(TopicAttributes attributes) {
98-
return getTopicEntity(attributes.getName());
99-
}
10095

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+
}
101103

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<>();
104107
}
105108

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+
}
110112

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+
}
114126

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+
}
132142

133143

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+
*/
134148
public void deleteTopic(String topicName) {
135149

136-
Assumption.assertNotNull(Const.StatusCodes.DBLEVEL_NULL_INPUT, topicName);
150+
Assumption.assertNotNull(Const.StatusCodes.DBLEVEL_NULL_INPUT, topicName);
137151

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());
142156

143157

144158
}

src/main/java/teammates/storage/entity/Topic.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111
import teammates.common.util.TimeHelper;
1212

1313
/**
14-
* Represents a course entity.
14+
* Represents a topic entity.
1515
*/
1616
@Entity
1717
@Index
1818
public class Topic extends BaseEntity {
1919

20+
/**
21+
* It is a must to set the @Id for the entity, thus GAE can use it to access database
22+
*/
2023
@Id
2124
private String name;
2225
private String desc;

0 commit comments

Comments
 (0)