15 package cz.vutbr.fit.knot.annotations.modules.SECDictionaryInterface;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Iterator;
33 import java.util.List;
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
37 import org.json.simple.JSONArray;
38 import org.json.simple.JSONObject;
39 import org.json.simple.JSONValue;
54 public static final String
PREFIX_NEW =
"https://www.googleapis.com/freebase/v1/image/";
56 public static final String
POSTFIX_NEW =
"?maxwidth=500&maxheight=500";
58 public static final String
PREFIX =
"api.freebase.com/api/trans/raw/";
67 public static final ArrayList<String>
NAME_ATTRIBUTES =
new ArrayList<String>(Arrays.asList(
87 private final static ArrayList<String>
ALLOWED_POSTFIX =
new ArrayList<String>(Arrays.asList(
113 super(
"SECDInterfaceThread");
115 request = requestInfo.getEntityRequest();
124 String messageForClient =
"";
125 ArrayList<EntityAttribute> resultsArray = null;
131 Iterator<EntityAttribute> resultIt = resultsArray.iterator();
132 while(resultIt.hasNext()){
134 if(((
Entity)currentAttr.getValue()).getVisualRepresentation()!= null){
135 String uri = ((
Entity)currentAttr.
getValue()).getVisualRepresentation();
137 String[] result = uri.split(
PREFIX);
138 if(result.length == 2){
139 String core = result[1];
143 currentAttr.setValue(currentValue);
152 if (resultsArray != null && !resultsArray.isEmpty()) {
156 if (messageForClient != null && !messageForClient.isEmpty()) {
157 requestInfo.getSession().addMessageTS(messageForClient);
158 requestInfo.getSession().notifyComet();
170 StringBuilder resultBuilder =
new StringBuilder();
172 resultBuilder.append(
"{ \"getEntities\":\n");
173 resultBuilder.append(
"{ \"type\":\n");
174 resultBuilder.append(
"\"");
175 resultBuilder.append(request.getType());
176 resultBuilder.append(
"\",\n");
177 resultBuilder.append(
" \"filter\":\n");
178 resultBuilder.append(
"\"");
179 resultBuilder.append(request.getFilter());
180 resultBuilder.append(
"\",\n");
181 resultBuilder.append(
" \"user\":\n");
182 resultBuilder.append(
"\"");
183 resultBuilder.append(requestInfo.getSession().getUser().getLogin());
184 resultBuilder.append(
"\",\n");
186 resultBuilder.append(
"\"userGroups\":[\n");
187 Iterator<UserGroup> groupIt = requestInfo.getSession().getUser().getGroups().iterator();
188 while(groupIt.hasNext()){
190 resultBuilder.append(
"{\"uri\":");
191 resultBuilder.append(
"\"");
193 resultBuilder.append(
"\",\n");
194 resultBuilder.append(
"\"name\":");
195 resultBuilder.append(
"\"");
196 resultBuilder.append(actualGroup.getName());
197 if(groupIt.hasNext()){
198 resultBuilder.append(
"\"},\n");
201 resultBuilder.append(
"\"}\n");
204 resultBuilder.append(
"],\n");
205 resultBuilder.append(
" \"userFrom\":\n");
206 resultBuilder.append(
"\"");
208 resultBuilder.append(requestInfo.getSession().getUser().getComeFrom());
210 resultBuilder.append(
"");
212 resultBuilder.append(
"\"}}\n");
215 return resultBuilder.toString();
225 StringBuilder resultString =
new StringBuilder();
226 Iterator<EntityAttribute> resultIt = responses.iterator();
228 Integer limit = request.getMaxResults();
231 if(limit == null || limit == -1){
237 resultString.append(
"<entities>");
238 while(resultIt.hasNext()){
240 if (currentEntity.
getStringValue() == null || currentEntity.getStringValue().isEmpty()) {
243 if (currentEntity.
getUri() == null || currentEntity.getUri().isEmpty()) {
246 resultString.append(currentEntity.toXmlString());
248 resultString.append(
"</entities>");
254 resultString.append(
"\" type=\"").append(
request.
getType()).append(
"\">");
257 resultString.append(
"\">");
261 while(resultIt.hasNext()){
265 if (currentEntity.
getStringValue() == null || currentEntity.getStringValue().isEmpty()) {
268 if (currentEntity.
getUri() == null || currentEntity.getUri().isEmpty()) {
273 if(counter <= limit){
274 resultString.append(currentEntity.toXMLResponseStringV2());
281 resultString.append(
"</entities>");
284 return resultString.toString();
295 ArrayList<EntityAttribute> returnList =
new ArrayList<EntityAttribute>();
298 JSONObject jsonResponse = (JSONObject)JSONValue.parse(JSONString);
299 if (jsonResponse == null) {
301 String msg =
"Unable to parse received JSON string with vocabulary entities.";
302 Logger.getLogger(SECDInterfaceThread.class.getName()).log(Level.SEVERE, msg);
308 JSONArray jsonEntities = (JSONArray)jsonResponse.get(
"entities");
309 if (jsonEntities == null) {
311 String msg =
"Unable to get new suggestions data from received JSON.";
312 Logger.getLogger(SECDInterfaceThread.class.getName()).log(Level.SEVERE, msg);
316 else if (jsonEntities.isEmpty()) {
321 for(
int index = 0; index < jsonEntities.size(); index++){
323 JSONObject currentEntity = (JSONObject)jsonEntities.get(index);
334 uri = (String)currentEntity.get(
"uri");
336 catch (ClassCastException e) {
338 String msg =
"Entity uri is not a String type.";
339 Logger.getLogger(SECDInterfaceThread.class.getName()).log(Level.SEVERE, msg);
346 type = (String)currentEntity.get(
"type");
348 catch (ClassCastException e) {
350 String msg =
"Entity type is not a String type.";
351 Logger.getLogger(SECDInterfaceThread.class.getName()).log(Level.SEVERE, msg);
358 name = (String)currentEntity.get(
"name");
360 catch (ClassCastException e) {
362 String msg =
"Entity name is not a String type.";
363 Logger.getLogger(SECDInterfaceThread.class.getName()).log(Level.SEVERE, msg);
370 visualUri = (String)currentEntity.get(
"visual_representation");
372 catch (ClassCastException e) {
374 String msg =
"Entity uri of visual representation is not a String type.";
375 Logger.getLogger(SECDInterfaceThread.class.getName()).log(Level.SEVERE, msg);
382 description = (String)currentEntity.get(
"description");
384 catch (ClassCastException e) {
386 String msg =
"Entity uri of visual representation is not a String type.";
387 Logger.getLogger(SECDInterfaceThread.class.getName()).log(Level.SEVERE, msg);
394 attr.setEntityAdditionalAttributes(addAttrs);
395 returnList.add(attr);
410 String result = source;
412 if (result != null) {
413 result = result.replace(
"\\",
"\\\\");
414 result = result.replace(
"\"",
"\\\"");
415 result = result.replace(
"\n",
" ");
416 result = result.replace(
"\t",
" ");
428 ArrayList<EntityAttribute> retList =
new ArrayList<EntityAttribute>();
430 String secApiResults =
"";
440 JSONObject jsonData = (JSONObject) JSONValue.parse(secApiResults);
441 if (jsonData == null) {
443 String msg =
"List with dictionary entities was not returned from SEC API.";
444 Logger.getLogger(SECDInterfaceThread.class.getName()).log(Level.SEVERE, msg);
449 JSONArray jsonEntities = (JSONArray) jsonData.get(
"data");
455 if (jsonEntities == null) {
457 String msg =
"List with dictionary entities was not returned from SEC API.";
458 Logger.getLogger(SECDInterfaceThread.class.getName()).log(Level.SEVERE, msg);
463 int numOfEntities = jsonEntities.size();
464 for (
int i = 0; i < numOfEntities; i++) {
466 JSONObject entity = (JSONObject) jsonEntities.get(i);
467 Set keySet = entity.keySet();
468 Iterator kIt = keySet.iterator();
470 String type = kIt.next().toString();
471 JSONObject entityAttributes = (JSONObject) entity.get(type);
472 Iterator<String> namesIt = NAME_ATTRIBUTES.iterator();
474 while ((name == null || name.isEmpty()) && namesIt.hasNext()) {
475 name = (String) entityAttributes.get(namesIt.next());
479 if (visual_representation == null) {
482 if (visual_representation == null) {
483 visual_representation =
"";
486 if (!pictures.isEmpty()) {
487 visual_representation = pictures.get(0);
493 ea.setEntityAdditionalAttributes(addAttrs);
496 }
catch (Exception e) {
498 String msg =
"Unable to parse received dictionary entity.";
499 Logger.getLogger(SECDInterfaceThread.class.getName()).log(Level.SEVERE, msg, e);
504 }
catch (Exception e) {
506 String msg =
"Unable to parse received JSON string with dictionary entities.";
507 Logger.getLogger(SECDInterfaceThread.class.getName()).log(Level.SEVERE, msg, e);
522 StringBuilder result =
new StringBuilder();
523 result.append(
"{\"get_entities\":{");
525 result.append(
"\"input_string\": \"");
526 result.append(request.getFilter());
527 result.append(
"\",");
529 result.append(
"\"types_and_attributes\": {");
533 ArrayList<SecApiReqTypeDef> available = null;
535 available = AppBean.getReqTypeDefinitionsEA();
537 available = AppBean.getAvTypeDefinitions();
541 ArrayList<SecApiReqTypeDef> requested =
new ArrayList<SecApiReqTypeDef>();
542 String requestedType = request.getType();
543 requestedType.replace(AppBean.getBaseTypeUri(),
"");
544 if (requestedType != null && !requestedType.isEmpty()
545 && requestedType.startsWith(
"*") && requestedType.endsWith(
"*")) {
546 requestedType = requestedType.substring(1, requestedType.length() - 1);
547 Iterator<SecApiReqTypeDef> avIt = available.iterator();
548 while (avIt.hasNext()) {
554 }
else if (requestedType != null && !requestedType.isEmpty() && requestedType.endsWith(
"*")) {
555 requestedType = requestedType.substring(0, requestedType.length() - 1);
556 Iterator<SecApiReqTypeDef> avIt = available.iterator();
557 while (avIt.hasNext()) {
563 }
else if (requestedType != null && !requestedType.isEmpty() && requestedType.startsWith(
"*")) {
564 requestedType = requestedType.substring(1, requestedType.length());
565 Iterator<SecApiReqTypeDef> avIt = available.iterator();
566 while (avIt.hasNext()) {
572 }
else if (requestedType != null && !requestedType.isEmpty()) {
573 Iterator<SecApiReqTypeDef> avIt = available.iterator();
574 while (avIt.hasNext()) {
581 Iterator<SecApiReqTypeDef> avIt = available.iterator();
582 while (avIt.hasNext()) {
592 Iterator<SecApiReqTypeDef> reqIt = requested.iterator();
593 if (reqIt.hasNext()) {
595 result.append(r.getRequestString());
597 while (reqIt.hasNext()) {
600 result.append(r.getRequestString());
603 Iterator<SecApiReqTypeDef> reqIt = requested.iterator();
604 if (reqIt.hasNext()) {
608 while (reqIt.hasNext()) {
618 Object[] paramsP = {
"name", Constants.MAX_ENTITIES_SETTINGS_NAME};
619 @SuppressWarnings(
"unchecked")
620 List<ServerSetting> settingsList = AppBean.getPersistenceManager().queryDB(
"ServerSetting.findByName", paramsP);
621 if (settingsList != null && !settingsList.isEmpty()) {
622 if (!settingsList.get(0).getSettingValue().isEmpty()) {
624 maxResults = Integer.parseInt(settingsList.get(0).getSettingValue());
625 }
catch (Exception e) {
634 if(request.
getMaxResults() != null && request.getMaxResults() != -1){
635 maxResults = request.getMaxResults();
642 result.append(
"\"max_results\": ");
643 result.append(maxResults.toString());
646 return result.toString();
658 String SECAPIResults =
"";
660 Boolean processed =
false;
663 SECAPIResults = sai.makeRequest(secApiRequest);
665 }
catch (Exception ex) {
666 String message =
"SEC API call error : " + ex.getMessage();
667 Logger.getLogger(SECDInterfaceThread.class.getName()).log(Level.SEVERE, message);
671 if (SECAPIResults == null || SECAPIResults.isEmpty()) {
677 Logger.getLogger(SECDInterfaceThread.class.getName()).log(Level.SEVERE,
"Initialisation of SEC API failed!");
678 return SECAPIResults;
682 SuggestionManager.initReqTypes();
684 return SECAPIResults;
689 SECAPIResults = sai.makeRequest(secApiRequest);
691 }
catch (Exception ex) {
692 String message =
"SEC API call error : " + ex.getMessage();
693 Logger.getLogger(SECDInterfaceThread.class.getName()).log(Level.SEVERE, message);
694 return SECAPIResults;
698 return SECAPIResults;
709 String secApiRequest =
"";
710 String SECAPIResults =
"";
716 SuggestionManager.initReqTypesFromRemote(sac, null);
718 return SECAPIResults;
723 SECAPIResults = sac.sendReqToSECAPI(secApiRequest, null);
724 }
catch (Exception ex) {
725 String message =
"Remote SEC API call error : " + ex.getMessage();
726 Logger.getLogger(SECDInterfaceThread.class.getName()).log(Level.SEVERE, message);
729 return SECAPIResults;
739 String exceptionData = null;
741 exceptionData = (String) jsonData.get(
"exception");
742 }
catch (Exception e) {
744 if (exceptionData != null && !exceptionData.isEmpty()) {
746 String msg =
"SEC API returned exception: " + exceptionData;
747 Logger.getLogger(SECDInterfaceThread.class.getName()).log(Level.SEVERE, msg);
761 ArrayList<String> result =
new ArrayList<String>();
765 for(
int i = 0; i < values.length; i++){
766 String[] tmpVal = (values[i]).split(
"\\.",-1);
767 picture += values[i];
768 if(tmpVal.length > 1){
769 String tmpStr = tmpVal[tmpVal.length-1];
770 Iterator<String> strIt = ALLOWED_POSTFIX.iterator();
771 while(strIt.hasNext()){
772 if(tmpStr.equals(strIt.next())){
790 ArrayList<EntityAdditionalAttribute> result =
new ArrayList<EntityAdditionalAttribute>();
792 Integer index = null;
794 index = AppBean.getDesTypeDefinitionsEA().indexOf(pattern);
797 if (index != null && index >= 0) {
798 priorities = AppBean.getDesTypeDefinitionsEA().
get(index);
800 Set keys = entityAttributes.keySet();
801 Iterator keysIt = keys.iterator();
802 while(keysIt.hasNext()){
803 String actualKey = (String) keysIt.next().toString();
808 String value = (String)entityAttributes.get(actualKey);
811 newAdditionalAttr.setName(actualKey);
812 if (actualKey.endsWith(
"_uri") || actualKey.endsWith(
"_url") || actualKey.endsWith(
"ulan_id")) {
814 newAdditionalAttr.setType(
"URI");
816 newAdditionalAttr.setStringValue(value);
817 newAdditionalAttr.setRefEntityAttribute(parentAttr);
818 if (priorities != null) {
819 newAdditionalAttr.setPriority(priorities.getPriority(actualKey));
822 result.add(newAdditionalAttr);
static final int MAX_RESULTS
synchronized boolean init()
Class representing parameter of server settings.
static final boolean VISUAL_REPRESENTATION_URI_CHANGE
static final ArrayList< String > FORBIDDEN_ENTITY_TYPES
boolean checkException(JSONObject jsonData)
String makeMessageFromResult(ArrayList< EntityAttribute > responses)
Additional attribute of entity in annotation attribute.
static boolean isSecCommandEmpty()
ArrayList< String > getPicturesFromString(String string)
Singleton for storing global variables.
static final String URI_ATTRIBUTE
Class representing vocabulary entity attribute.
static final String ALT_VIS_REPRESENTATION_ATTRIBUTE
static final String POSTFIX_NEW
ArrayList< EntityAttribute > makeArrayFromResult(String JSONString)
Interface for call of SEC API as external program (deamon)
static final String DESCRIPTION_ATTRIBUTE
Class representing user group.
String createGERequest(EntityRequest request)
Holds information about the query to dictionary entries.
ArrayList< EntityAttribute > getResultFromSECAPI(EntityRequest request)
static final ArrayList< String > ALLOWED_POSTFIX
static ArrayList< SecApiReqTypeDef > getDesTypeDefinitionsEA()
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...
static final ArrayList< String > NAME_ATTRIBUTES
Class for creating of requested type definition for SEC API.
static final int LOG_LEVEL
static ArrayList< SecApiReqTypeDef > getReqTypeDefinitions()
String makeSECAPIRequest(EntityRequest request)
EditorSession getSession()
String makeRemoteSECAPIRequest(EntityRequest request)
static final String PREFIX
static final Boolean USE_REMOTE_SEC_API
static final String MULTIPLE_DATA_SEPARATOR
static final int LOG_LEVEL_SERVER_ERRORS
static final ArrayList< String > REQUESTED_ATTRIBUTES
static final int PROTOCOL_LOD_V2
Class implementing functions for communication with remote SEC API.
static ArrayList< SecApiReqTypeDef > getReqTypeDefinitionsEA()
SECDInterfaceThread(RequestInfo requestInfo)
Processed informations about client request.
Class holds values of dictionary entity.
String EscapeCharsToSend(String source)
ArrayList< EntityAdditionalAttribute > getAdditionalAttributes(JSONObject entityAttributes, EntityAttribute parentAttr)
String makeRequestString(EntityRequest request)
static final String PREFIX_NEW
static final double SEC_API_AUTOCOMPLETE_RESULTS_RESERVE
static final String VIS_REPRESENTATION_ATTRIBUTE