Mercurial > hg > LGServices
diff src/main/java/de/mpiwg/gazetteer/utils/DataProvider.java @ 41:ba9515f22897
new: topic management and adding sections from searching result into topic
author | Zoe Hong <zhong@mpiwg-berlin.mpg.de> |
---|---|
date | Thu, 17 Dec 2015 13:44:08 +0100 |
parents | 35ed4e650a53 |
children | 9dbbbfd474f4 |
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/gazetteer/utils/DataProvider.java Mon Dec 07 17:06:57 2015 +0100 +++ b/src/main/java/de/mpiwg/gazetteer/utils/DataProvider.java Thu Dec 17 13:44:08 2015 +0100 @@ -1,5 +1,6 @@ package de.mpiwg.gazetteer.utils; +import java.sql.SQLException; import java.util.ArrayList; import java.util.Collections; import java.util.Date; @@ -12,7 +13,10 @@ import de.mpiwg.gazetteer.bo.LGBranch; import de.mpiwg.gazetteer.bo.LGFile; import de.mpiwg.gazetteer.bo.LGFullTextSearchFile; +import de.mpiwg.gazetteer.bo.LGTopic; +import de.mpiwg.gazetteer.bo.LGTopicSectionRelation; import de.mpiwg.gazetteer.db.DBContents; +import de.mpiwg.gazetteer.db.DBSection; import de.mpiwg.gazetteer.utils.exceptions.NoAuthorizedException; import de.mpiwg.gazetteer.utils.exceptions.VersioningException; @@ -34,6 +38,11 @@ public DataProvider(){ logger.info("##### Starting DataProvider #####"); } + + + + /* --- branch --- */ + public LGFile getFile(Long fileId){ return getFileMap().getValuesByOwnKey(fileId); } @@ -259,6 +268,11 @@ } + /* --- end branch --- */ + + + + /* --- full text search --- */ public List<LGFullTextSearchFile> getSearchFileList4User(Long userId) { @@ -303,7 +317,179 @@ return file; } + /* --- end full text search --- */ + + /* --- topic --- */ + public List<LGTopic> getTopics(Long userId){ + List<LGTopic> list = new ArrayList<LGTopic>(); + for(LGTopic topic : getTopicMap().values()){ + if(topic.hasContributor(userId)){ + list.add(topic); + } + } + return list; + } + + public LGTopic getTopic(Long topicId){ + return getTopicMap().getValuesByOwnKey(topicId); + } + + + public void deleteTopic(LGTopic topic){ + + int modifiedRelation = DBService.deleteTopicFromDB(topic.getId()); + getTopicMap().remove(topic.getKey()); + + logger.info("removing " + modifiedRelation + " records in topicSectionRelation of topic " + topic.toString()); + + this.setTopicSectionRelationMap(null); // clear topicSectionRelationMap cache + + + } + + public void createTopic(String nameEn, String nameCh,String namePinyin, String description, Long userId) { + Date date = new Date(); + + LGTopic topic = new LGTopic(); + topic.setNameEn(nameEn); + topic.setNameCh(nameCh); + topic.setNamePinyin(namePinyin); + topic.setDescription(description); + topic.setUserId(userId); + topic.setContributors("[" + userId.toString() + "]"); + + //Saving into DB + //################################## + // For Topic table + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + DBService.saveDBEntry0(session, topic, date); + + session.getTransaction().commit(); + //################################## + + } + + public void updateTopic(LGTopic topic) throws Exception { + if(!topic.isPersistent()){ + throw new Exception("Trying to update a topic that it is not persistent!"); + } + + Date date = new Date(); + DBService.saveDBEntry(topic, date); + this.getTopicMap().put(topic.getKey(), topic); + + } + + + + public List<DBSection> getAllSectionsInTopic(Long topicId) throws SQLException{ + + List<LGTopicSectionRelation> topicSectionRelationList = getTopicSectionRelationMap().getValuesByAKey(topicId); + + // get sections from topicSecionRelation list + List<DBSection> list = new ArrayList<DBSection>(); + for (LGTopicSectionRelation aRelation : topicSectionRelationList) { + logger.debug(aRelation.getInfo() ); + + DBSection section = DBService.getSectionWithContent(aRelation.getSectionId()); + list.add(section); + + } + + + Collections.sort(list); + Collections.reverse(list); + return list; + } + + + + + public void deleteTopicSectionRelation(Long topicId, String bookId) throws Exception{ + // delete record with topicId, bookId in topicSectionRelation + // this means the sectionId = 0, there is no corresponding section in the book for this topic + + // TODO + + } + + public void deleteTopicSectionRelation(Long topicId, Long sectionId) throws Exception{ + if (sectionId == 0) { + throw new Exception("sectionId cannot be 0"); + } + + DuplexKey<Long, Long> key = new DuplexKey<Long, Long>(topicId, sectionId); + LGTopicSectionRelation topicSectionRelation = this.getTopicSectionRelationMap().get(key); + + LGTopic topic = getTopic(topicId); + + if(topic == null){ + throw new Exception("There is no topic of " + topicSectionRelation); + } + + //deleting topicSectionRelation from DB and cache + int modifiedRelation = DBService.deleteTopicSectionRelationFromDB(topicSectionRelation.getId()); + getTopicSectionRelationMap().remove(topicSectionRelation.getKey()); + + logger.info(modifiedRelation + " items deleted by removing topicSectionRelation " + topicSectionRelation.toString()); + + + } + + public String updateTopicSectionRelation(Long sectionId, Long topicId) throws Exception { + DuplexKey<Long, Long> key = new DuplexKey<Long, Long>(topicId, sectionId); + LGTopicSectionRelation topicSectionRelation = this.getTopicSectionRelationMap().get(key); + + if (topicSectionRelation != null) { + // the relation already existed + // would this possibly need any modification to the existing record in the topicSectionRelation table? + + return "updated"; + + } else { + createTopicSectionRelation(sectionId, topicId); + + return "added"; + } + + + + } + + + public void createTopicSectionRelation(Long sectionId, Long topicId) throws Exception { + + Date date = new Date(); + + LGTopicSectionRelation topicSectionRelation = new LGTopicSectionRelation(); + topicSectionRelation.setTopicId(topicId); + topicSectionRelation.setSectionId(sectionId); + + DBService.getInstance(); + DBSection section = DBService.getSectionWithContent(sectionId); + topicSectionRelation.setBookId(section.getBookId()); + + //Saving into DB + //################################## + // For Topic table + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + DBService.saveDBEntry0(session, topicSectionRelation, date); + + session.getTransaction().commit(); + //################################## + + // update cache + this.loadTopicSectionRelations(); + + } + + /* --- end topic --- */ + }