4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
SECDInterfaceModule.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: SECDInterfaceModule.java
5  * Description: Class implements AnotServerModule calss for prupose of SEC Dictionary module.
6  */
7 
8 /**
9  * @file SECDInterfaceModule.java
10  *
11  * @brief Class provides connection of SEC Dictionary module with server.
12  */
13 
14 /**
15  * @package cz.vutbr.fit.knot.annotations.modules.SECDictionaryInterface
16  *
17  * @brief Classes responsible for connection to the controlled vocabulary
18  */
19 package cz.vutbr.fit.knot.annotations.modules.SECDictionaryInterface;
20 
31 import java.util.ArrayList;
32 import java.util.Iterator;
33 import java.util.logging.Level;
34 import java.util.logging.Logger;
35 
36 
37 /**
38  * Class implements AnotServerModule calls for prupose of SEC Dictionary module.
39  *
40  * @brief Class provides connection of SEC Dictionary module with server.
41  * @author Martin Petr (xpetrm05)
42  */
43 public class SECDInterfaceModule implements AnnotServerModule {
44 
45  /**
46  * This method is called when client sent message and these message
47  * has been processed. Changes in types, annotations and in other entities
48  * are persisted after calling this method from all modules.
49  *
50  * @param requestInfo Informations about client request
51  * @return String with messages for client or empty string
52  */
53  @Override
54  public String processRequestBeforePersist(RequestInfo requestInfo) {
55  if(requestInfo.getEntityRequest() != null){
56  // if there is some request for entities
57  SECDInterfaceThread thread = new SECDInterfaceThread(requestInfo);
58  thread.start();
59  }
60  return "";
61  }
62 
63  /**
64  * This method is called when client sent message, these message
65  * has been processed and changes in entities are persisted.
66  * Composed response is sent to client after calling this method from
67  * all modules.
68  * It performs:
69  * - processing of request for entity types
70  *
71  * @param requestInfo Informations about client request
72  * @param persistFailed True if changes not persisted, false otherwise.
73  * @return String with messages for client or empty string
74  */
75  @Override
76  public String processRequestAfterPersist(RequestInfo requestInfo, boolean persistFailed) {
77  StringBuilder response = new StringBuilder();
78 
79  // Check whether user asked for entity types
80  if(requestInfo.isGetEntityTypes()){
81  response.append(processGetEntityTypes(requestInfo));
82  }
83 
84  return response.toString();
85  } // processRequestAfterPersist()
86 
87  /**
88  * This method is called for each sleeping comet handler when he receives
89  * flier (part of informations about this or another client request).
90  *
91  * @param flier Flier, which has been sent to all comet handlers.
92  * @param session Session associated with comet handler.
93  * @return If this handler hasn't react to this flier (not interested) return
94  * null, else return messages for client in single string.
95  */
96  @Override
97  public String messagesFromFlier(Flier flier, EditorSession session) {
98  return "";
99  }
100 
101  /**
102  * Prepares response for request for entity types.
103  *
104  * @param requestInfo Informations about client request
105  * @return Returns response for client
106  */
107  private String processGetEntityTypes(RequestInfo requestInfo) {
108 
109  // initialisation of arrays with types
110  if (initArrays(requestInfo)) {
111  return "";
112  }
113 
114  // Getting list of entity types
115  Iterator<SecApiReqTypeDef> avIt;
116  if (AppBean.getReqTypeDefinitionsEA() != null && !AppBean.getReqTypeDefinitionsEA().isEmpty()) {
117  avIt = AppBean.getReqTypeDefinitionsEA().iterator();
118  } else {
119  avIt = AppBean.getAvTypeDefinitions().iterator();
120  }
121 
122  //Creating messages with entity types got from SECApi
123  String message = "";
124 
125  if (avIt.hasNext()) {
126  message = "<entityTypes>\n";
127 
128  while (avIt.hasNext()) {
129  SecApiReqTypeDef td = avIt.next();
130  if (!Constants.FORBIDDEN_ENTITY_TYPES.contains(td.getTypeName())) {
131  message += " <type name=\"" + td.getTypeName() + "\"";
132  if (td.getAltDescDef() != null) {
133  message += " description=\"" + td.getAltDescDef() + "\"/>\n";
134  } else {
135  message += "/>\n";
136  }
137  }
138  }
139  message += "</entityTypes>\n";
140  }
141 
142  return message;
143 
144  } // processGetEntityTypes()
145 
146  /**
147  * Initialization of arrays
148  * Remote - always
149  * Local - sometimes
150  *
151  * @param requestInfo Info about client's request
152  *
153  * @return In case of success returns false, true otherwise
154  */
155  private boolean initArrays(RequestInfo requestInfo) {
156  // Remote
158  try {
159  SECAPIConn sac = new SECAPIConn();
160  SuggestionManager.initReqTypesFromRemote(sac, requestInfo);
161  } catch (Exception e) {
162  String message = "SECApi call error: " + e.getMessage();
163  Logger.getLogger(SECDInterfaceModule.class.getName()).log(Level.SEVERE, message);
164  }
165  //Init has failed
166  if (AppBean.getAvTypeDefinitions() == null || AppBean.getAvTypeDefinitions().isEmpty()) {
167  Logger.getLogger(SECDInterfaceModule.class.getName()).log(Level.WARNING, "SECApi has failed or connection timeout!");
168  return true;
169  }
170  } //Local
171  else {
172  // If it's not initialized
173  if (AppBean.getAvTypeDefinitions() == null || AppBean.getAvTypeDefinitions().isEmpty()) {
174  try {
175  SuggestionManager.initReqTypes();
176  } catch (Exception e) {
177  String logMessage = "SECApi call error: " + e.getMessage();
178  Logger.getLogger(SECDInterfaceModule.class.getName()).log(Level.SEVERE, logMessage);
179  }
180 
181  //Init has failed
182  if (AppBean.getAvTypeDefinitions() == null || AppBean.getAvTypeDefinitions().isEmpty()) {
183  Logger.getLogger(SECDInterfaceModule.class.getName()).log(Level.WARNING, "SECApi has failed!");
184  return true;
185  }
186  }
187  }
188  return false; // succsee
189  } // initArrays()
190 
191  /**
192  * Returns name of this module. Name will be included in XML element
193  * attribute so usable characters are restricted.
194  *
195  * @param lang Requested language of name
196  * @return Name of this module
197  */
198  @Override
199  public String getModuleName(int lang) {
200  return "SECDInterfaceModule";
201  }
202 
203  /**
204  * Returns description of this module. It will be part of information
205  * about server functions.
206  *
207  * @param lang Requested language of description
208  * @return Description of this module
209  */
210  @Override
211  public String getModuleDescription(int lang) {
212  return "Module provides communication between 4A Server and SEC Store API for"+ "prupose of controlled dictionary.";
213  }
214 
215 } // public class SECDInterfaceModule
String processRequestAfterPersist(RequestInfo requestInfo, boolean persistFailed)
static final ArrayList< String > FORBIDDEN_ENTITY_TYPES
Definition: Constants.java:432
Singleton for storing global variables.
Definition: AppBean.java:47
Class provides connection of SEC Dictionary module with server.
Interface for call of SEC API as external program (deamon)
static ArrayList< SecApiReqTypeDef > getAvTypeDefinitions()
Definition: AppBean.java:917
Class provides offerining of suggestions with usage of local knowledge repository.
Class forwards queries from anotation client to the SEC API server and and creates a message for anno...
Class for creating of requested type definition for SEC API.
Flier with informations for comet handlers.
Definition: Flier.java:31
Class implementing functions for communication with remote SEC API.
Definition: SECAPIConn.java:37
static ArrayList< SecApiReqTypeDef > getReqTypeDefinitionsEA()
Definition: AppBean.java:972
Processed informations about client request.
Informations about client session.