comparison src/main/java/de/mpiwg/web/BranchEditor.java @ 0:7682c04c63a8

First commit of the source code!
author "jurzua <jurzua@mpiwg-berlin.mpg.de>"
date Tue, 10 Mar 2015 14:50:41 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:7682c04c63a8
1 package de.mpiwg.web;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.faces.event.ActionEvent;
7
8 import org.apache.commons.lang.SerializationUtils;
9 import org.apache.log4j.Logger;
10
11 import de.mpiwg.gazetteer.bo.LGBranch;
12 import de.mpiwg.gazetteer.bo.LGFile;
13 import de.mpiwg.gazetteer.dataverse.DataverseUtils;
14 import de.mpiwg.gazetteer.dataverse.bo.VDCUser;
15 import de.mpiwg.gazetteer.utils.DataProvider;
16 import de.mpiwg.gazetteer.utils.FileManager;
17 import de.mpiwg.gazetteer.utils.SelectableObject;
18
19 public class BranchEditor extends AbstractBean{
20
21
22 private static Logger logger = Logger.getLogger(BranchEditor.class);
23
24 private LGBranch currentBranch;
25 private LGFile currentLastFile;
26 private List<LGFile> allFiles;
27 private String text;
28 private List<VDCUser> suggestionUserList;
29 private List<SelectableObject<VDCUser>> contributors;
30
31 public void loadBranch(Long branchId){
32 LGBranch branch = DataProvider.getInstance().getBranch(branchId);
33 this.loadBranch(branch);
34 }
35
36 public void loadBranch(LGBranch branch){
37 logger.info("Loading Branch: " + branch.toString());
38 this.reset();
39 if(branch != null && branch.isPersistent()){
40
41 try {
42 this.currentBranch = (LGBranch)branch.clone();
43 this.currentLastFile = DataProvider.getInstance().getFile(branch.getCurrentLastFileId());
44 this.allFiles = DataProvider.getInstance().getAllFiles(branch.getId());
45 this.text = FileManager.getFile(this.currentLastFile);
46 this.contributors = new ArrayList<SelectableObject<VDCUser>>();
47 for(Long userId : this.currentBranch.getContributorsList()){
48 VDCUser user = DataverseUtils.getUser(userId);
49 if(user != null){
50 this.contributors.add(new SelectableObject<VDCUser>(user));
51 }
52
53 }
54 //for(this.currentBranch.getContributorsList())
55 } catch (Exception e) {
56 internalError(e);
57 }
58
59 logger.info("allFiles.size=" + allFiles.size());
60 }
61 }
62
63 public void reset(){
64 this.currentBranch = null;
65 this.currentLastFile = null;
66 this.allFiles = null;
67 }
68
69 public void listenerShowContributorsDialog(ActionEvent event){
70 this.suggestionUserList = new ArrayList<VDCUser>();
71 try {
72 for(VDCUser user : DataverseUtils.getAllUsers()){
73 if(!currentBranch.hasContributor(user.getId())){
74 this.suggestionUserList.add(user);
75 }
76 }
77 } catch (Exception e) {
78 internalError(e);
79 }
80
81
82 }
83
84 public void listenerCloseContributorsDialog(ActionEvent event){
85 this.suggestionUserList = null;
86 }
87
88 public void listenerUpdateBranch(ActionEvent event){
89 this.saveBranch0();
90 }
91
92 public void listenerAddContributor(ActionEvent event){
93 VDCUser user = (VDCUser)getRequestBean("contributor");
94 if(user != null){
95 this.suggestionUserList = null;
96 this.currentBranch.addContributor(user.getId());
97 }
98
99 this.saveBranch0();
100 }
101
102 public void listenerRemoveContributor(ActionEvent event){
103 List<VDCUser> toDelete = new ArrayList<VDCUser>();
104 for(SelectableObject<VDCUser> so : new ArrayList<SelectableObject<VDCUser>>(this.contributors)){
105 if(so.isSelected()){
106 toDelete.add(so.getObj());
107 this.contributors.remove(so);
108 }
109 }
110
111 for(VDCUser user : toDelete){
112 this.currentBranch.removeContributor(user.getId());
113 }
114
115 this.saveBranch0();
116
117 }
118
119 private void saveBranch0(){
120 try {
121 DataProvider.getInstance().updateBranch(currentBranch);
122 this.loadBranch(currentBranch);
123 addMsg("The branch has been updated!");
124 } catch (Exception e) {
125 internalError(e);
126 }
127 }
128
129 public void listenerSaveText(ActionEvent event){
130 try {
131 LGFile newFile = DataProvider.getInstance().saveFile(currentBranch.getId(), this.text, getSessionBean().getUser().getId(), this.currentLastFile.getId());
132 LGBranch branch = DataProvider.getInstance().getBranch(newFile.getBranchId());
133 this.loadBranch(branch);
134 addMsg("New File create " + newFile.getId());
135 } catch (Exception e) {
136 internalError(e);
137 }
138 }
139
140 public LGBranch getCurrentBranch() {
141 return currentBranch;
142 }
143
144 public void setCurrentBranch(LGBranch currentBranch) {
145 this.currentBranch = currentBranch;
146 }
147
148 public LGFile getCurrentLastFile() {
149 return currentLastFile;
150 }
151
152 public void setCurrentLastFile(LGFile currentLastFile) {
153 this.currentLastFile = currentLastFile;
154 }
155
156 public List<LGFile> getAllFiles() {
157 return allFiles;
158 }
159
160 public void setAllFiles(List<LGFile> allFiles) {
161 this.allFiles = allFiles;
162 }
163
164 public String getText() {
165 return text;
166 }
167
168 public void setText(String text) {
169 this.text = text;
170 }
171
172 public List<VDCUser> getSuggestionUserList() {
173 return suggestionUserList;
174 }
175
176 public void setSuggestionUserList(List<VDCUser> suggestionUserList) {
177 this.suggestionUserList = suggestionUserList;
178 }
179
180 public List<SelectableObject<VDCUser>> getContributors() {
181 return contributors;
182 }
183
184 public void setContributors(List<SelectableObject<VDCUser>> contributors) {
185 this.contributors = contributors;
186 }
187 }