Mercurial > hg > LGServer
view src/main/java/de/mpiwg/gazetteer/utils/DataProvider.java @ 5:5316e79f9a27
Implementation of search pagination and lazy loading to display the result set of a search.
author | "jurzua <jurzua@mpiwg-berlin.mpg.de>" |
---|---|
date | Mon, 16 Mar 2015 11:25:36 +0100 |
parents | 7682c04c63a8 |
children |
line wrap: on
line source
package de.mpiwg.gazetteer.utils; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; import org.apache.log4j.Logger; import org.hibernate.Session; import de.mpiwg.gazetteer.bo.LGBranch; import de.mpiwg.gazetteer.bo.LGFile; import de.mpiwg.gazetteer.utils.exceptions.NoAuthorizedException; import de.mpiwg.gazetteer.utils.exceptions.VersioningException; public class DataProvider extends AbstractDataProvider{ private static Logger logger = Logger.getLogger(DBService.class); private static DataProvider instance; public static DataProvider getInstance(){ if(instance == null) instance = new DataProvider(); return instance; } public DataProvider(){ logger.info("##### Starting DataProvider #####"); } public LGFile getFile(Long fileId){ return getFileMap().getValuesByOwnKey(fileId); } public List<LGFile> getAllFiles(Long branchId){ List<LGFile> list = getFileMap().getValuesByAKey(branchId); Collections.sort(list); Collections.reverse(list); return list; } public List<LGBranch> getBranches(Long userId){ List<LGBranch> list = new ArrayList<LGBranch>(); for(LGBranch branch : getBranchMap().values()){ if(branch.hasContributor(userId)){ list.add(branch); } } return list; } public LGBranch getBranch(Long branchId){ return getBranchMap().getValuesByOwnKey(branchId); } public void deleteBranch(LGBranch branch){ int modifiedFiles = DBService.deleteBranchFromDB(branch.getId()); List<LGFile> fileToDelete = getFileMap().getValuesByAKey(branch.getId()); for(LGFile file : new ArrayList<LGFile>(fileToDelete)){ getFileMap().remove(file.getKey()); } getBranchMap().remove(branch.getKey()); logger.info(modifiedFiles + " items deleted by removing branch " + branch.toString()); } public void updateBranch(LGBranch branch) throws Exception{ if(!branch.isPersistent()){ throw new Exception("Trying to update a branch that it is not persistent!"); } Date date = new Date(); DBService.saveDBEntry(branch, date); this.getBranchMap().put(branch.getKey(), branch); } public LGFile saveFile(Long branchId, String text, Long userId, Long userPreviousFileId) throws Exception{ Date date = new Date(); LGBranch branch = getBranchMap().getValuesByOwnKey(branchId); if(!branch.hasContributor(userId)){ throw new NoAuthorizedException(userId, branchId); } if(!branch.getCurrentLastFileId().equals(userPreviousFileId)){ LGFile userPreviousFile = getFileMap().getValuesByOwnKey(userPreviousFileId); LGFile currentLastFile = getFileMap().getValuesByOwnKey(branch.getCurrentLastFileId()); throw new VersioningException(userPreviousFile, currentLastFile); } LGFile previousFile = getFileMap().getValuesByOwnKey(branch.getCurrentLastFileId()); LGFile file = new LGFile(); file.setBranchId(branchId); file.setVersion(previousFile.getVersion() + 1); file.setUserId(userId); file.setContent(text); //Saving into DB //################################## Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.getTransaction().begin(); file.setBranchId(branch.getId()); DBService.saveDBEntry0(session, file, date); previousFile.setLastVersion(false); DBService.saveDBEntry0(session, previousFile, date); branch.setCurrentLastFileId(file.getId()); DBService.saveDBEntry0(session, branch, date); //Saving physical file in the operating system String fileName = FileManager.saveFile(branch, file, date); file.setFileName(fileName); DBService.saveDBEntry0(session, file, date); session.getTransaction().commit(); //################################## //Saving into Cache getBranchMap().put(branch.getKey(), branch); getFileMap().put(file.getKey(), file); getFileMap().put(previousFile.getKey(), previousFile); return file; } public Long saveNewFile(String text, String label, Long sectionId, Long userId) throws Exception{ Date date = new Date(); LGBranch branch = new LGBranch(); branch.setSectionId(sectionId); branch.setUserId(userId); branch.setLabel(label); branch.addContributor(userId); branch.loadTransientData(); LGFile file = new LGFile(); file.setUserId(userId); file.setContent(text); //Saving into DB //################################## Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.getTransaction().begin(); DBService.saveDBEntry0(session, branch, date); file.setBranchId(branch.getId()); DBService.saveDBEntry0(session, file, date); branch.setCurrentLastFileId(file.getId()); //Saving physical file in the operating system String fileName = FileManager.saveFile(branch, file, date); file.setFileName(fileName); DBService.saveDBEntry0(session, file, date); session.getTransaction().commit(); //################################## //Saving into Cache getBranchMap().put(branch.getKey(), branch); getFileMap().put(file.getKey(), file); return branch.getId(); } }