15 package cz.vutbr.fit.knot.annotations.modules.StoryscopeInterface;
32 import java.io.BufferedReader;
33 import java.io.InputStreamReader;
34 import java.io.OutputStreamWriter;
36 import java.net.URLConnection;
37 import java.net.URLEncoder;
38 import java.util.ArrayList;
39 import java.util.Iterator;
40 import java.util.List;
41 import java.util.logging.Level;
42 import java.util.logging.Logger;
43 import org.w3c.dom.Document;
68 private static final String
answerTemplate =
"<?xml version=\"1.0\" encoding=\"utf-8\"?><ok/>";
76 super(
"StoryscopeInterfaceSender");
173 AnnotDocument doc = requestInfo.getSession().getSyncDocument();
190 AppBean.getLockMaster().getDocumentLock(doc.
getId()).unlockForRead();
197 AppBean.getLockMaster().getDocumentLock(doc.
getId()).unlockForRead();
209 @SuppressWarnings(
"unchecked")
210 List<StoryScope> tmpList = AppBean.getPersistenceManager().getEntitiesByName(
"StoryScope");
214 Iterator<StoryScope> ssListIterator = storyScopes.iterator();
216 while(ssListIterator.hasNext()){
217 StoryScope actualStoryScope = ssListIterator.next();
218 ArrayList<SubscribedSource> subscribedList = actualStoryScope.getSubscribedList();
219 ArrayList<SubscribedSource> unsubscribedList = actualStoryScope.getUnsubscribedList();
222 ArrayList<Annotation> refusedAnnotations =
new ArrayList<Annotation>();
223 ArrayList<Annotation> acceptedAnnotations =
new ArrayList<Annotation>();
224 Iterator addAnnotIterator = addedAnnotations.iterator();
226 while(addAnnotIterator.hasNext()){
230 acceptedAnnotations.add(actualAnnotation);
232 refusedAnnotations.add(actualAnnotation);
237 acceptedAnnotations =
getRelatedAnnotations(acceptedAnnotations,refusedAnnotations,subscribedList,unsubscribedList);
238 ArrayList<String> addMessage =
new ArrayList<String>();
239 Iterator<Annotation> acceptedIterator = acceptedAnnotations.iterator();
240 while(acceptedIterator.hasNext())
242 addMessage.add(
AnnotationToXMLStringL(acceptedIterator.next(), requestInfo.getSession().getParsedSyncDocument()));
255 Iterator<Annotation> autoUpdatedIter = requestInfo.getFlier().getAutoUpdatedAnnotations().iterator();
258 while(autoUpdatedIter.hasNext()){
259 Annotation actualAnnotation = autoUpdatedIter.next();
262 editedAnnotations.add(actualAnnotation);
268 refusedAnnotations =
new ArrayList<Annotation>();
269 acceptedAnnotations =
new ArrayList<Annotation>();
270 Iterator editedAnnotIterator = editedAnnotations.iterator();
272 while(editedAnnotIterator.hasNext()){
276 acceptedAnnotations.add(actualAnnotation);
278 refusedAnnotations.add(actualAnnotation);
283 acceptedAnnotations =
getRelatedAnnotations(acceptedAnnotations,refusedAnnotations,subscribedList,unsubscribedList);
284 ArrayList<String> editedMessage =
new ArrayList<String>();
285 acceptedIterator = acceptedAnnotations.iterator();
286 while(acceptedIterator.hasNext())
288 editedMessage.add(
AnnotationToXMLStringL(acceptedIterator.next(), requestInfo.getSession().getParsedSyncDocument()));
292 refusedAnnotations =
new ArrayList<Annotation>();
293 acceptedAnnotations =
new ArrayList<Annotation>();
294 Iterator removedAnnotIterator = removedAnnotations.iterator();
296 while(removedAnnotIterator.hasNext()){
300 acceptedAnnotations.add(actualAnnotation);
302 refusedAnnotations.add(actualAnnotation);
307 acceptedAnnotations =
getRelatedAnnotations(acceptedAnnotations,refusedAnnotations,subscribedList,unsubscribedList);
308 ArrayList<String> removedMessage =
new ArrayList<String>();
309 acceptedIterator = acceptedAnnotations.iterator();
310 while(acceptedIterator.hasNext())
312 removedMessage.add(
AnnotationToXMLStringL(acceptedIterator.next(), requestInfo.getSession().getParsedSyncDocument()));
316 if(!addMessage.isEmpty() || !editedMessage.isEmpty() || !removedMessage.isEmpty())
322 String data =
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"
323 +
"<messages xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\""
324 +
" xmlns:geo=\"http://www.w3.org/2003/01/geo/wgs84_pos#\""
325 +
" xmlns:a=\"http://knot.fit.vutbr.cz/annotations/AnnotXMLSchema\">\n"
337 actualStoryScope.deleteMessages();
352 String answer =
new String();
355 String postMessage =
"xml=" + URLEncoder.encode(data,
"UTF-8");
358 URLConnection clientConnection = clientUrl.openConnection();
359 clientConnection.setDoOutput(
true);
361 OutputStreamWriter dataWriter =
new OutputStreamWriter(clientConnection.getOutputStream());
362 dataWriter.write(postMessage);
365 BufferedReader recivedData =
new BufferedReader(
new InputStreamReader(clientConnection.getInputStream()));
367 while ((line = recivedData.readLine()) != null) {
375 String msg =
"Unable to send data to StoryScope with name: " + storyScope.getName() +
".";
376 Logger.getLogger(StoryscopeInterfaceSender.class.getName()).log(Level.SEVERE, msg);
383 String msg =
"A confirmation message from StoryScope with name: " + storyScope.getName() +
" not match. Message content:" + answer;
384 Logger.getLogger(StoryscopeInterfaceSender.class.getName()).log(Level.SEVERE, msg);
406 ArrayList<SubscribedSource> subscribedSources,ArrayList<String> sources,ArrayList<SubscribedSource> unsubResult,
407 ArrayList<SubscribedSource> subResult){
411 while(actualType != null)
413 int ancestorsCount = actualType.getAncestorTypes().size();
415 if(ancestorsCount <= 1){
418 unsubResult.addAll(EditorSession.listOfMatches(unsubscribedSources,annot,actualType,sources));
422 subResult.addAll(EditorSession.listOfMatches(subscribedSources,annot,actualType,sources));
424 if(!subResult.isEmpty() && EditorSession.containTypeRule(subResult)){
427 if(!unsubResult.isEmpty() && EditorSession.containTypeRule(unsubResult)){
432 actualType = actualType.getAncestorType();
434 Iterator<AnnotType> typeIt = actualType.getAncestorTypes().iterator();
435 int resultsArray[] =
new int [ancestorsCount];
436 boolean unsubscribedOccur =
false;
438 for(
int i = 0;typeIt.hasNext();i++){
439 resultsArray[i] =
checkBranch(typeIt.next(),annot,unsubscribedSources,subscribedSources,sources,unsubResult,subResult);
461 private boolean isSubscribed(
Annotation annot,ArrayList<SubscribedSource> unsubscribedSources, ArrayList<SubscribedSource> subscribedSources, ArrayList<String> sources) {
463 ArrayList<SubscribedSource> unsubResult =
new ArrayList<SubscribedSource>();
464 ArrayList<SubscribedSource> subResult =
new ArrayList<SubscribedSource>();
466 switch(
checkBranch(annot.
getAnnotType(),annot,unsubscribedSources,subscribedSources,sources,unsubResult,subResult)){
467 case Constants.SUBSCRIBED:
return true;
468 case Constants.UNSUBSCRIBED:
return false;
474 if(!subResult.isEmpty() || !unsubResult.isEmpty())
500 ArrayList<Annotation> innerAnnotationList =
new ArrayList<Annotation>();
501 Iterator <BaseAttribute> attrIt = annot.getAttributes().iterator();
503 while(attrIt.hasNext()){
508 LinkedAnnotationAttribute linkedAttr = (LinkedAnnotationAttribute)attrTmp;
513 return innerAnnotationList;
523 ArrayList<Annotation> innerAnnotationList =
new ArrayList<Annotation>();
524 Object[] params = {
"linked",annot.getId()};
525 @SuppressWarnings(
"unchecked")
526 List <LinkedAnnotationAttribute> attrList = AppBean.getPersistenceManager().queryDB(
"Attribute.findByLinked", params);
529 Iterator <LinkedAnnotationAttribute> attrIt = attrList.iterator();
531 while(attrIt.hasNext()){
535 innerAnnotationList.add(dbAnnot);
539 return innerAnnotationList;
552 private ArrayList<Annotation>
decomposeLinked(ArrayList<Annotation> mainAnnotList, ArrayList<Annotation> secondaryAnnotList,
Annotation annot){
555 found :
while(innerIt.hasNext()){
558 Iterator<Annotation> mainIt = mainAnnotList.iterator();
559 while(mainIt.hasNext()){
562 if(approvedAnnot.
getId() == actualAnnot.getId()){
568 Iterator<Annotation> secondIt = secondaryAnnotList.iterator();
569 while(secondIt.hasNext()){
572 if(approvedAnnot.
getId() == actualAnnot.getId()){
576 secondaryAnnotList.add(actualAnnot);
579 return secondaryAnnotList;
594 ArrayList<Annotation> unapprovedAnnot,
595 ArrayList<SubscribedSource> subscribed,
596 ArrayList<SubscribedSource> unsubscribed){
598 ArrayList<Annotation> secondaryApproved =
getRelatedAnnots(approvedAnnot,unapprovedAnnot,subscribed,unsubscribed);
599 Iterator<Annotation> secApprovedIt = secondaryApproved.iterator();
602 while(secApprovedIt.hasNext()){
604 ArrayList<Annotation> openList =
new ArrayList<Annotation>();
606 ArrayList<Annotation> closeList =
new ArrayList<Annotation>();
607 openList.add(secApprovedIt.next());
608 while(!openList.isEmpty()){
610 closeList.add(actualAnnot);
616 Iterator<Annotation> linksIt = links.iterator();
617 while(linksIt.hasNext()){
619 if(!openList.contains(actualLink) && !closeList.contains(actualLink)){
620 openList.add(actualLink);
621 if(!addedAnnotations.contains(actualLink) && !approvedAnnot.contains(actualLink))
623 addedAnnotations.add(actualLink);
632 if(approvedAnnot == null){
637 return approvedAnnot;
650 ArrayList<Annotation> unapprovedAnnot,
651 ArrayList<SubscribedSource> subscribed,
652 ArrayList<SubscribedSource> unsubscribed) {
653 ArrayList<Annotation> result =
new ArrayList<Annotation>();
654 Iterator<Annotation> unapprovedIt = unapprovedAnnot.iterator();
656 while (unapprovedIt.hasNext()) {
658 ArrayList<Annotation> openList =
new ArrayList<Annotation>();
660 ArrayList<Annotation> closeList =
new ArrayList<Annotation>();
661 openList.add(unapprovedIt.next());
663 while (!openList.isEmpty()) {
665 closeList.add(actualAnnot);
668 result.add(actualAnnot);
672 ArrayList<Annotation> annotForAdd = CoreFuncModule.getDifference(CoreFuncModule.getDifference(CoreFuncModule.getLinkedAnnotationsDB(actualAnnot), closeList), openList);
673 openList.addAll(annotForAdd);
678 if(approvedAnnot == null){
682 approvedAnnot.addAll(result);
683 return approvedAnnot;
693 StringBuilder result =
new StringBuilder();
694 ArrayList<AnnotType> openList =
new ArrayList<AnnotType>();
695 ArrayList<AnnotType> closeList =
new ArrayList<AnnotType>();
697 openList.add(annot.getAnnotType());
698 while(!openList.isEmpty()){
699 AnnotType actualAnnot = openList.remove(0);
700 if(!closeList.contains(actualAnnot)){
701 closeList.add(actualAnnot);
702 result.append(
"<ancestorType uri=\"").append(actualAnnot.
getUri()).append(
"\"/>");
703 openList.addAll(actualAnnot.getAncestorTypes());
707 return result.toString();
724 String authorAttr =
"";
726 authorAttr = authorAttr +
" name=\"" + annot.getAuthorName() +
"\"";
729 authorAttr = authorAttr +
" address=\"" + annot.getAuthorAddress() +
"\"";
731 String aboutStr =
"";
732 if (annot.
getId() != null) {
733 aboutStr = AppBean.getBaseAnnotUri() + annot.
getId();
736 String OntologyUri =
"";
738 OntologyUri =
" ontologyUri=\"" + annot.getAnnotType().getUriInOntology() +
"\"";
741 String typeComment =
new String();
742 String tmpComent =
"";
745 tmpComent = annot.getAnnotType().getComment();
750 typeComment =
"<comment><![CDATA["
751 + annot.getAnnotType().getComment()
755 return "<annotation>"
756 +
"<rdf:Description rdf:about=\"" + aboutStr +
"\">"
757 +
"<rdf:type rdf:resource=\"" + annot.getAnnotType().getUri() + OntologyUri +
"\">"
763 +
"<a:dateTime rdf:value=\"" + Util.toRFC3339Date(annot.getCreated()) +
"\" />"
764 +
"<a:author id=\"" + annot.getAuthorIdStr() +
"\"" + authorAttr +
"/>"
765 +
"<a:source rdf:resource=\"" + annot.
getSource() +
"\" uri=\"" + annot.getSourceDocument().getUri() +
"\"/>"
773 +
"</rdf:Description>"
785 String LinearizedFragments =
"";
786 ArrayList<ArrayList<Fragment>> notConverted =
new ArrayList<ArrayList<Fragment>>();
787 ArrayList<ArrayList<Fragment>> comFragments =
new ArrayList<ArrayList<Fragment>>();
788 comFragments.add(
new ArrayList<Fragment>());
789 Iterator<Fragment> convIt = annot.getFragments().iterator();
790 while(convIt.hasNext()){
791 comFragments.get(0).add(convIt.next());
794 ArrayList<Fragment> linearizedList;
795 linearizedList = Linearizer.fragmentsToLinFragments(comFragments,doc, notConverted,
false);
797 if (notConverted != null) {
798 Iterator<ArrayList<Fragment>> badFrIt = notConverted.iterator();
799 while (badFrIt.hasNext()) {
800 ArrayList<Fragment> badFrListForAnnot = badFrIt.next();
801 Iterator<Fragment> badFrItForAnnot = badFrListForAnnot.iterator();
802 while (badFrItForAnnot.hasNext()) {
803 Fragment fragment = badFrItForAnnot.next();
804 String msg =
"This fragment was not linearized successfully: ";
805 msg = msg +
" offset: " + fragment.getOffset()
806 +
" length: " + fragment.
getLength() +
" text: " + fragment.getAnnotatedText();
807 Logger.getLogger(StoryscopeInterfaceSender.class.getName()).log(Level.SEVERE, msg);
812 Iterator<Fragment> fragIt = linearizedList.iterator();
814 while (fragIt.hasNext()) {
818 LinearizedFragments += fr.toXMLString();
821 return LinearizedFragments;
834 String attrString =
"";
835 Iterator<BaseAttribute> attrIt = annot.getAttributes().iterator();
837 while (attrIt.hasNext()) {
838 attr = attrIt.next();
840 Annotation nestedAnnot = ((NestedAnnotationAttribute)attr).getNestedAnnotation();
841 if(nestedAnnot != null){
842 attrString +=
"<a:attribute name=\"" + attr.getName() +
"\" type=\"nestedAnnotation\" "
843 +
"tmpId=\"" + nestedAnnot.
getTmpId() +
"\">";
847 attrString +=
"<a:comment> <![CDATA[" + attr.getComment() +
"]]></a:comment>";
849 attrString +=
"</a:attribute>";
852 attrString += attr.toXMLString(proto11, tmpIdForNested,
true);
ArrayList< Annotation > getLinkedAnnotations(Annotation annot)
ArrayList< Annotation > getRelatedAnnotations(ArrayList< Annotation > approvedAnnot, ArrayList< Annotation > unapprovedAnnot, ArrayList< SubscribedSource > subscribed, ArrayList< SubscribedSource > unsubscribed)
static LockMaster getLockMaster()
Utility functions for document linearization.
static final int MESSAGE_CHANGE
ArrayList< Annotation > getAddedAnnotations()
StoryscopeInterfaceSender(RequestInfo requestInfo)
Class represent thread of Storyscope Interface that sends messages to StoryScopes.
void setAddedAnnotations(ArrayList< Annotation > addedAnnotations)
ArrayList< Annotation > getRemovedAnnotations()
String AnnotationTypeAncestors(Annotation annot)
Class representing annotated copy of document.
Document, annotations and types manipulation functionality.
Class representing attribute of type NestedAnnotation.
Class representing StoryScope for needs of SEC Interface.
ArrayList< Annotation > editedAnnotations
Singleton for storing global variables.
String getAuthorAddress()
ArrayList< Annotation > decomposeLinked(ArrayList< Annotation > mainAnnotList, ArrayList< Annotation > secondaryAnnotList, Annotation annot)
Class representing attribute of type AnnotationLink.
static boolean containSourceRule(ArrayList< SubscribedSource > list)
String fragmentsToXMLStringL(Annotation annot, Document doc)
ArrayList< Annotation > getEditedAnnotations()
StoryscopeInterfaceSender(String name)
static ArrayList< String > getUserSources(User user)
static final int SUBSCRIBED
ArrayList< Annotation > addedAnnotations
boolean sendAndCheck(StoryScope storyScope, String data)
static String messagesToString(ArrayList< String > messages)
Base class representing attribute of annotation.
ArrayList< Annotation > getLinkedAnnotationsDB(Annotation annot)
static final int LOG_LEVEL
void setEditedAnnotations(ArrayList< Annotation > editedAnnotations)
Class representing type of annotation.
ArrayList< Annotation > getRelatedAnnots(ArrayList< Annotation > approvedAnnot, ArrayList< Annotation > unapprovedAnnot, ArrayList< SubscribedSource > subscribed, ArrayList< SubscribedSource > unsubscribed)
EditorSession getSession()
static PersistM getPersistenceManager()
void setRemovedAnnotations(ArrayList< Annotation > removedAnnotations)
String AnnotationToXMLStringL(Annotation annot, Document doc)
static final int LOG_LEVEL_SERVER_ERRORS
static final int MESSAGE_DELETE
ArrayList< Annotation > removedAnnotations
Processed informations about client request.
boolean isSubscribed(Annotation annot, ArrayList< SubscribedSource > unsubscribedSources, ArrayList< SubscribedSource > subscribedSources, ArrayList< String > sources)
String attributesToXMLStringL(Annotation annot, boolean proto11, boolean tmpIdForNested, Document doc)
static boolean containUserRule(ArrayList< SubscribedSource > list)
List< StoryScope > storyScopes
Subscribed source of annotations.
Informations about client session.
Utility class (manipulates RFC 3339 dates)
Class representing annotated fragment.
static final int MESSAGE_ADD
Class representing annotation.
static final String answerTemplate
int checkBranch(AnnotType rootType, Annotation annot, ArrayList< SubscribedSource > unsubscribedSources, ArrayList< SubscribedSource > subscribedSources, ArrayList< String > sources, ArrayList< SubscribedSource > unsubResult, ArrayList< SubscribedSource > subResult)
static final int UNSUBSCRIBED