comparison src/main/java/de/mpiwg/gazetteer/utils/DataProvider.java @ 0:3e62083dbcbf

First commit. This project comes from LGServer. We removed the framework icefaces. Now, LGServices uses just JSP and jquery.
author "jurzua <jurzua@mpiwg-berlin.mpg.de>"
date Thu, 23 Apr 2015 15:46:01 +0200
parents
children ce2e3f2814c0
comparison
equal deleted inserted replaced
-1:000000000000 0:3e62083dbcbf
1 package de.mpiwg.gazetteer.utils;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Date;
6 import java.util.List;
7
8 import org.apache.log4j.Logger;
9 import org.hibernate.Session;
10
11 import de.mpiwg.gazetteer.bo.LGBranch;
12 import de.mpiwg.gazetteer.bo.LGFile;
13 import de.mpiwg.gazetteer.utils.exceptions.NoAuthorizedException;
14 import de.mpiwg.gazetteer.utils.exceptions.VersioningException;
15
16 public class DataProvider extends AbstractDataProvider{
17
18 private static Logger logger = Logger.getLogger(DBService.class);
19 private static DataProvider instance;
20
21 public static DataProvider getInstance(){
22 if(instance == null)
23 instance = new DataProvider();
24 return instance;
25 }
26
27 public DataProvider(){
28 logger.info("##### Starting DataProvider #####");
29 }
30
31 public LGFile getFile(Long fileId){
32 return getFileMap().getValuesByOwnKey(fileId);
33 }
34
35 public List<LGFile> getAllFiles(Long branchId){
36 List<LGFile> list = getFileMap().getValuesByAKey(branchId);
37 Collections.sort(list);
38 Collections.reverse(list);
39 return list;
40 }
41
42 public List<LGBranch> getBranches(Long userId){
43 List<LGBranch> list = new ArrayList<LGBranch>();
44 for(LGBranch branch : getBranchMap().values()){
45 if(branch.hasContributor(userId)){
46 list.add(branch);
47 }
48 }
49 return list;
50 }
51
52 public LGBranch getBranch(Long branchId){
53 return getBranchMap().getValuesByOwnKey(branchId);
54 }
55
56 public void deleteFile(LGFile file) throws Exception{
57
58 LGBranch branch = getBranch(file.getBranchId());
59
60 if(branch == null){
61 throw new Exception("There is any Branch for " + file);
62 }
63
64 List<LGFile> files = getAllFiles(file.getBranchId());
65
66 if(files.size() == 1){
67 throw new Exception("This file could not be deleted, because it is part of a branch that has only one file. A Branch without files is a inconsistency.");
68 }
69
70 // if the file is the last version of a branch, we must replace the current file with the penultimate file.
71 if(branch.getCurrentLastFileId().equals(file.getId())){
72 LGFile penultimateFile = getPenultimateFile(files);
73
74 penultimateFile.setLastVersion(true);
75 branch.setCurrentLastFileId(penultimateFile.getId());
76
77 this.updateBranch(branch);
78 this.updateFile(penultimateFile);
79
80 }
81
82 //deleting file from DB and cache
83 int modifiedFiles = DBService.deleteFileFromDB(file.getId());
84 getFileMap().remove(file.getKey());
85
86 logger.info(modifiedFiles + " items deleted by removing file " + file);
87 }
88
89 private LGFile getPenultimateFile(List<LGFile> files){
90 LGFile penultimateFile = null;
91 LGFile lastFile = null;
92
93 for(LGFile file : files){
94 if(lastFile == null){
95 lastFile = file;
96 }else if(penultimateFile == null){
97 lastFile = (lastFile.getVersion() > file.getVersion()) ? lastFile : file;
98 penultimateFile = (lastFile.getVersion() < file.getVersion()) ? lastFile : file;
99 }else{
100 if(file.getVersion() > lastFile.getVersion()){
101 penultimateFile = lastFile;
102 lastFile = file;
103 }else if(file.getVersion() > penultimateFile.getVersion()){
104 penultimateFile = file;
105 }
106 }
107 }
108
109 return penultimateFile;
110 }
111
112 public void deleteBranch(LGBranch branch){
113
114 int modifiedFiles = DBService.deleteBranchFromDB(branch.getId());
115 List<LGFile> fileToDelete = getFileMap().getValuesByAKey(branch.getId());
116
117 for(LGFile file : new ArrayList<LGFile>(fileToDelete)){
118 getFileMap().remove(file.getKey());
119 }
120 getBranchMap().remove(branch.getKey());
121
122 logger.info(modifiedFiles + " items deleted by removing branch " + branch.toString());
123 }
124
125 public void updateFile(LGFile file) throws Exception{
126 if(!file.isPersistent()){
127 throw new Exception("Trying to update a file that it is not persistent!");
128 }
129
130 Date date = new Date();
131 DBService.saveDBEntry(file, date);
132 this.getFileMap().put(file.getKey(), file);
133 }
134
135 public void updateBranch(LGBranch branch) throws Exception{
136 if(!branch.isPersistent()){
137 throw new Exception("Trying to update a branch that it is not persistent!");
138 }
139
140 Date date = new Date();
141 DBService.saveDBEntry(branch, date);
142 this.getBranchMap().put(branch.getKey(), branch);
143 }
144
145 public LGFile saveFile(Long branchId, String text, Long userId, Long userPreviousFileId) throws Exception{
146
147
148
149 Date date = new Date();
150
151 LGBranch branch = getBranchMap().getValuesByOwnKey(branchId);
152
153 if(!branch.hasContributor(userId)){
154 throw new NoAuthorizedException(userId, branchId);
155 }
156
157 if(!branch.getCurrentLastFileId().equals(userPreviousFileId)){
158 LGFile userPreviousFile = getFileMap().getValuesByOwnKey(userPreviousFileId);
159 LGFile currentLastFile = getFileMap().getValuesByOwnKey(branch.getCurrentLastFileId());
160
161 throw new VersioningException(userPreviousFile, currentLastFile);
162 }
163
164 LGFile previousFile = getFileMap().getValuesByOwnKey(branch.getCurrentLastFileId());
165
166 LGFile file = new LGFile();
167 file.setBranchId(branchId);
168 file.setVersion(previousFile.getVersion() + 1);
169 file.setUserId(userId);
170 file.setContent(text);
171
172 //Saving into DB
173 //##################################
174 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
175 session.getTransaction().begin();
176
177
178 file.setBranchId(branch.getId());
179 DBService.saveDBEntry0(session, file, date);
180
181 previousFile.setLastVersion(false);
182 DBService.saveDBEntry0(session, previousFile, date);
183
184 branch.setCurrentLastFileId(file.getId());
185 DBService.saveDBEntry0(session, branch, date);
186
187 //Saving physical file in the operating system
188 String fileName = FileManager.saveFile(branch, file, date, userId);
189 file.setFileName(fileName);
190 DBService.saveDBEntry0(session, file, date);
191
192 session.getTransaction().commit();
193 //##################################
194
195
196 //Saving into Cache
197 getBranchMap().put(branch.getKey(), branch);
198 getFileMap().put(file.getKey(), file);
199 getFileMap().put(previousFile.getKey(), previousFile);
200
201 return file;
202 }
203
204 public Long saveNewFile(String text, String label, Long sectionId, Long userId) throws Exception{
205
206 Date date = new Date();
207
208 LGBranch branch = new LGBranch();
209 branch.setSectionId(sectionId);
210 branch.setUserId(userId);
211 branch.setLabel(label);
212 branch.addContributor(userId);
213 branch.loadTransientData();
214
215 LGFile file = new LGFile();
216 file.setUserId(userId);
217 file.setContent(text);
218
219 //Saving into DB
220 //##################################
221 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
222 session.getTransaction().begin();
223
224 DBService.saveDBEntry0(session, branch, date);
225 file.setBranchId(branch.getId());
226
227 DBService.saveDBEntry0(session, file, date);
228 branch.setCurrentLastFileId(file.getId());
229
230 //Saving physical file in the operating system
231 String fileName = FileManager.saveFile(branch, file, date, userId);
232 file.setFileName(fileName);
233 DBService.saveDBEntry0(session, file, date);
234
235 session.getTransaction().commit();
236 //##################################
237
238
239 //Saving into Cache
240 getBranchMap().put(branch.getKey(), branch);
241 getFileMap().put(file.getKey(), file);
242
243 return branch.getId();
244
245 }
246 }