4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
SetServerURI.java
Go to the documentation of this file.
1 /*
2  * Project: Server for annotations sharing
3  * Author: Ing. Jaroslav Dytrych idytrych@fit.vutbr.cz
4  * File: SetServerURI.java
5  * Description: Backbean for page for setting of server URI
6  */
7 
8 /**
9  * @file SetServerURI.java
10  *
11  * @brief Backbean for page for setting of server URI
12  */
13 
14 package cz.vutbr.fit.knot.annotations.web;
15 
26 import java.io.Serializable;
27 import java.util.Iterator;
28 import java.util.List;
29 import javax.faces.bean.ManagedBean;
30 import javax.faces.bean.SessionScoped;
31 import javax.persistence.EntityManager;
32 import javax.persistence.EntityTransaction;
33 import javax.persistence.Query;
34 
35 /**
36  * Backbean for page with administration of users
37  *
38  * @brief Backbean for administration of users
39  * @author idytrych
40  */
41 @ManagedBean
42 @SessionScoped
43 public class SetServerURI implements Serializable {
44 
45  /**
46  * Value of New URI field in form
47  */
48  private String newURI = null;
49  /**
50  * Error message displayed in form
51  */
52  private String errorMessage = "";
53 
54  /**
55  * Constructor
56  */
57  public SetServerURI() {
58  }
59 
60  /**
61  * Gets user selected in the table
62  *
63  * @return Returns user selected in the table
64  */
65  public String getCurrentUri() {
66  return AppBean.getServerUri();
67  }
68 
69  /**
70  * Gets value for New URI field
71  *
72  * @return Returns value for new URI field
73  */
74  public String getNewUri() {
75  return newURI;
76  }
77 
78  /**
79  * Sets value for New URI field
80  *
81  * @param newURI Value for New URI field
82  */
83  public void setNewUri(String newURI) {
84  this.newURI = newURI;
85  }
86 
87  /**
88  * Action listener for save button
89  *
90  * @return Returns page outcome (identificator of next page or null to stay here)
91  */
92  public String btnSaveAction() {
93  if(!SessionManager.getSession().isLoggedIn()){
94  return "login";
95  }
96  errorMessage = "";
97 
98  if (newURI == null || newURI.isEmpty()) {
99  errorMessage = errorMessage + MessageProvider.getMessage("uriCanNotBeEmpty") + " ";
100  return null;
101  }
102 
103  String oldURI = AppBean.getServerUri();
104  AppBean.setServerUri(newURI); // set new URI
105 
106  EntityManager em = AppBean.getPersistenceManager().getEM();
107  EntityTransaction transaction = em.getTransaction();
108  try {
109  transaction.begin();
110 
111  Query q = em.createNamedQuery("Annotation.findAll");
112  List aList = q.getResultList();
113  if (aList != null && !aList.isEmpty()) {
114  Iterator aIt = aList.iterator();
115  while (aIt.hasNext()) {
116  Annotation a = (Annotation) aIt.next();
117  String src = a.getSource();
118  String aid = a.getAuthorIdStr();
119  if (src != null && src.startsWith(oldURI)) {
120  src = newURI + src.substring(oldURI.length(), src.length());
121  a.setSource(src);
122  }
123  if (aid != null && aid.startsWith(oldURI)) {
124  aid = newURI + aid.substring(oldURI.length(), src.length());
125  a.setAuthorIdStr(aid);
126  }
127  em.merge(a);
128  }
129  }
130 
131  q = em.createNamedQuery("AnnotType.findAll");
132  aList = q.getResultList();
133  if (aList != null && !aList.isEmpty()) {
134  Iterator aIt = aList.iterator();
135  while (aIt.hasNext()) {
136  AnnotType a = (AnnotType) aIt.next();
137  String uri = a.getUri();
138  String oUri = a.getUriInOntology();
139  if (uri != null && uri.startsWith(oldURI)) {
140  uri = newURI + uri.substring(oldURI.length(), uri.length());
141  a.setUri(uri);
142  }
143  if (oUri != null && oUri.startsWith(oldURI)) {
144  oUri = newURI + oUri.substring(oldURI.length(), oUri.length());
145  a.setUriInOntology(oUri);
146  }
147  em.merge(a);
148  }
149  }
150 
151  q = em.createNamedQuery("AnnotTypeAttr.findAll");
152  aList = q.getResultList();
153  if (aList != null && !aList.isEmpty()) {
154  Iterator aIt = aList.iterator();
155  while (aIt.hasNext()) {
156  AnnotTypeAttr a = (AnnotTypeAttr) aIt.next();
157  String oUri = a.getUriInOntology();
158  if (oUri != null && oUri.startsWith(oldURI)) {
159  oUri = newURI + oUri.substring(oldURI.length(), oUri.length());
160  a.setUriInOntology(oUri);
161  }
162  em.merge(a);
163  }
164  }
165 
166  q = em.createNamedQuery("StoredDocument.findAll");
167  aList = q.getResultList();
168  if (aList != null && !aList.isEmpty()) {
169  Iterator aIt = aList.iterator();
170  while (aIt.hasNext()) {
171  StoredDocument d = (StoredDocument) aIt.next();
172  String oUri = d.getUri();
173  if (oUri != null && oUri.startsWith(oldURI)) {
174  oUri = newURI + oUri.substring(oldURI.length(), oUri.length());
175  d.setUri(oUri);
176  }
177  em.merge(d);
178  }
179  }
180 
181  q = em.createNamedQuery("StoredDocument.findAll");
182  aList = q.getResultList();
183  if (aList != null && !aList.isEmpty()) {
184  Iterator aIt = aList.iterator();
185  while (aIt.hasNext()) {
186  SubscribedItem i = (SubscribedItem) aIt.next();
187  String oUri = i.getSource();
188  if (oUri != null && oUri.startsWith(oldURI)) {
189  oUri = newURI + oUri.substring(oldURI.length(), oUri.length());
190  i.setSource(oUri);
191  }
192  em.merge(i);
193  }
194  }
195 
196  q = em.createNamedQuery("Suggestion.findAll");
197  aList = q.getResultList();
198  if (aList != null && !aList.isEmpty()) {
199  Iterator aIt = aList.iterator();
200  while (aIt.hasNext()) {
201  Suggestion a = (Suggestion) aIt.next();
202  String src = a.getSource();
203  String aid = a.getAuthorIdStr();
204  if (src != null && src.startsWith(oldURI)) {
205  src = newURI + src.substring(oldURI.length(), src.length());
206  a.setSource(src);
207  }
208  if (aid != null && aid.startsWith(oldURI)) {
209  aid = newURI + aid.substring(oldURI.length(), src.length());
210  a.setAuthorIdStr(aid);
211  }
212  em.merge(a);
213  }
214  }
215 
216  q = em.createNamedQuery("TypeAttrOnto.findAll");
217  aList = q.getResultList();
218  if (aList != null && !aList.isEmpty()) {
219  Iterator aIt = aList.iterator();
220  while (aIt.hasNext()) {
221  TypeAttrOnto a = (TypeAttrOnto) aIt.next();
222  String oUri = a.getUriInOntology();
223  if (oUri != null && oUri.startsWith(oldURI)) {
224  oUri = newURI + oUri.substring(oldURI.length(), oUri.length());
225  a.setUriInOntology(oUri);
226  }
227  em.merge(a);
228  }
229  }
230 
231  q = em.createNamedQuery("ServerSetting.findByName");
232  q.setParameter("name", Constants.SERVER_URI_SETTING_NAME);
233  aList = q.getResultList();
234  ServerSetting s = null;
235  if (aList != null && !aList.isEmpty()) {
236  s = (ServerSetting) aList.get(0);
237  s.setSettingValue(newURI);
238  } else {
240  }
241  em.merge(s);
242 
243  em.flush();
244  transaction.commit();
245  } catch (Exception e) {
246  transaction.rollback();
247  AppBean.setServerUri(oldURI);
248  errorMessage = errorMessage + MessageProvider.getMessage("dbError") + " ";
249  return null;
250  } finally {
251  em.close();
252  }
253 
254  return "serverSettingList";
255  } // btnSaveAction()
256 
257  /**
258  * Action listener for cancel button
259  *
260  * @return Returns page outcome (identificator of next page or null to stay here)
261  */
262  public String btnCancelAction() {
263  if(!SessionManager.getSession().isLoggedIn()){
264  return "login";
265  } else return "serverSettingList";
266  }
267 
268  /**
269  * Gets error message displayed in form
270  * @return Returns error message displayed in form
271  */
272  public String getErrorMessage() {
273  return errorMessage;
274  }
275 
276 } // public class SetServerURI
277 
Backbean for administration of users.
Class representing attribute of unknown type of annotation.
Class representing parameter of server settings.
Class representing attribute of type of annotation.
Class for manipulating with session.
Singleton for storing global variables.
Definition: AppBean.java:47
Class representing type of annotation.
Definition: AnnotType.java:58
Class representing item of subscripted or unsubscripted sources list.
Class representing suggestion of annotation.
Definition: Suggestion.java:87