19 package cz.vutbr.fit.knot.annotations.modules.suggestionManager.alternative;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.List;
37 import java.util.logging.Level;
38 import java.util.logging.Logger;
39 import javax.persistence.EntityManager;
40 import javax.persistence.EntityTransaction;
41 import javax.persistence.Query;
85 this.requestInfo = request;
88 this.newTypes =
new ArrayList<AnnotType>();
110 }
catch(Exception ex){
111 String msg =
"Unknown exception in AlternativeManager: " + ex.getMessage();
112 Logger.getLogger(AlternativeManager.class.getName()).log(Level.SEVERE, msg, ex);
115 AppBean.getLockMaster().getDocumentLock(
docID).unlockForRead();
125 if(
AppBean.
getLockMaster().getSuggReqLock(requestInfo.getSession().getSyncDocument().getId()).lockForChange()){
131 SuggestionManager.SuggestionSource suggestionSource = SuggestionManager.sourceOfSuggestions(
document);
134 String rawNewAlternatives = null;
143 if (rawNewAlternatives != null) {
144 ArrayList<Suggestion> alternativesCreatedFor =
new ArrayList<Suggestion>();
147 sugManager.lockDocForChange(
docID);
154 if (newAlternatives != null && !newAlternatives.isEmpty()) {
160 if (newTypes != null) {
161 Iterator<AnnotType> newTypesUpdateIt = newTypes.iterator();
162 while (newTypesUpdateIt.hasNext()) {
163 AnnotType currentNewType = newTypesUpdateIt.next();
165 Iterator<Alternative> newAlternativesIt = newAlternatives.iterator();
166 while (newAlternativesIt.hasNext()) {
167 Alternative currentAlternative = newAlternativesIt.next();
169 currentAlternative.setAnnotType(currentNewType);
176 if (requestInfo.getSession().getAutoActivateAlternatives()) {
177 Iterator<Alternative> newAlternativesIt = newAlternatives.iterator();
178 while (newAlternativesIt.hasNext()) {
179 Alternative currentAlternative = newAlternativesIt.next();
181 alternativesCreatedFor.add(currentAlternative.getAlternativeOfSuggestion());
188 Iterator<EditorSession> sessIt = AppBean.getSessions().iterator();
189 while (sessIt.hasNext()) {
192 sess.addMessageTS(typesStr);
196 String msg =
"Persisting of types of annotations failed.";
197 Logger.getLogger(AlternativeManager.class.getName()).log(Level.SEVERE, msg);
201 }
catch (Exception ex) {
202 String msg =
"Unknown exception in AlternativeManager: " + ex.getMessage();
203 Logger.getLogger(AlternativeManager.class.getName()).log(Level.SEVERE, msg, ex);
207 sugManager.unlockDocForChange(
docID);
211 Iterator<Suggestion> newAltIt = alternativesCreatedFor.iterator();
212 while (newAltIt.hasNext()) {
216 String result = sugManager.printSuggestionsDiff(requestInfo.getSession(),
new ArrayList<Suggestion>(),
new ArrayList<AnnotType>());
217 requestInfo.getSession().addMessageTS(result);
218 requestInfo.getSession().notifyComet();
223 AppBean.getLockMaster().getSuggReqLock(
requestInfo.
getSession().getSyncDocument().getId()).unlockForChange();
236 int topAlternativeOfId;
237 Suggestion alternativeOf = sug.getAlternativeOfSuggestion();
240 boolean suggIsAlternativeOfAnotherSuggestion =
true;
241 if (alternativeOf == null) {
242 topAlternativeOfId = sug.getId();
243 suggIsAlternativeOfAnotherSuggestion =
false;
245 topAlternativeOfId = alternativeOf.getId();
249 List<Alternative> alternativeList =
getAlternatives(topAlternativeOfId);
252 ArrayList<Alternative> alternativeToPersist =
new ArrayList<Alternative>();
253 if (alternativeList.isEmpty()) {
257 if (alternativeList.size() == 1 || suggIsAlternativeOfAnotherSuggestion) {
259 alternativeToPersist.add(alternativeList.get(0));
262 for (
int i = 0; i < Constants.MAX_COUNT_OF_ALTERNATIVES_IN_BATCH && i < alternativeList.size(); i++) {
263 alternativeToPersist.add(alternativeList.get(i));
267 ArrayList<Alternative> open =
new ArrayList<Alternative>(alternativeToPersist);
268 ArrayList<Alternative> closed =
new ArrayList<Alternative>();
270 while(!open.isEmpty()){
272 ArrayList<Alternative> tmpList =
new ArrayList<Alternative>();
275 Iterator<AlternativeAttribute> attIt = tmpAlt.getAttributes().iterator();
276 while(attIt.hasNext()){
278 if(tmpAtt.
getUri() != null && !tmpAtt.getUri().isEmpty()){
282 if(tmpAtt.
getSimpleType().equalsIgnoreCase(
"NestedSuggestion")){
283 a = tmpAtt.getNestedAlternative();
285 else if(tmpAtt.
getSimpleType().equalsIgnoreCase(
"SuggestionLink")){
286 a = tmpAtt.getLinkedAlternative();
290 a.setTmpId(tmpAtt.getUri());
292 if(!open.contains(a) && !closed.contains(a)){
298 String msg =
"Alternative attribute with name: "
300 +
"does not contain reference to linked/nested alternative.";
301 Logger.getLogger(AlternativeManager.class.getName()).log(Level.WARNING, msg);
308 open.addAll(tmpList);
316 HashMap<String, Suggestion> sugsMap =
new HashMap<String, Suggestion>();
317 Iterator<Suggestion> tmpSugIt = sugAlternativeToPersist.iterator();
318 while(tmpSugIt.hasNext()){
320 if(s.
getTmpId() != null && !s.getTmpId().isEmpty()){
321 sugsMap.put(s.getTmpId(), s);
326 tmpSugIt = sugAlternativeToPersist.iterator();
327 while(tmpSugIt.hasNext()){
330 Iterator<SugBaseAttribute> sbAttIt = s.getAttributes().iterator();
331 while(sbAttIt.hasNext()){
333 if(sbAtt.
getSimpleType().equalsIgnoreCase(
"NestedSuggestion")){
334 sbAtt.setNestedSuggestion(sugsMap.get(sbAtt.getUri()));
336 else if(sbAtt.
getSimpleType().equalsIgnoreCase(
"SuggestionLink")){
337 sbAtt.setLinkedSuggestion(sugsMap.get(sbAtt.getUri()));
343 for (Iterator<Suggestion> it = sugAlternativeToPersist.iterator(); it.hasNext(); ) {
345 SuggestionManager.updateSuggestion(actualSuggestion,request);
347 Iterator<SuggestionFragment> fragIt = actualSuggestion.getFragments().iterator();
348 while(fragIt.hasNext()){
354 String msg =
"Some alternative fragment can not be recovered.";
355 Logger.getLogger(AlternativeManager.class.getName()).log(Level.WARNING, msg);
363 SugPersister.persist(request, null, null, sugAlternativeToPersist,
false,
new ArrayList<AnnotType>());
379 ArrayList<Alternative> alternativeToDelete =
new ArrayList<Alternative>();
380 ArrayList<Suggestion> suggestionsToRefuse =
new ArrayList<Suggestion>();
381 for (Iterator<Suggestion> it = confirmSuggs.iterator(); it.hasNext(); ) {
383 Integer sugId = sg.getId();
384 Suggestion alternativeOfSug = sg.getAlternativeOfSuggestion();
385 Integer alternativeOfId = (alternativeOfSug != null ? alternativeOfSug.getId() : null);
386 if (alternativeOfId != null && sugId != null) {
390 }
else if (sugId != null) {
397 if (!alternativeToDelete.isEmpty()) {
412 if (request.
getSession() == null || request.getSession().getDefaultGroup() == null) {
414 String msg =
"Missing default group in refusing process.";
415 Logger.getLogger(AlternativeManager.class.getName()).log(Level.WARNING, msg);
419 Integer groupId = request.getSession().getDefaultGroup().getId();
420 for (Iterator<Suggestion> it = suggestionsToRefuse.iterator(); it.hasNext(); ) {
421 SuggestionManager.updateSuggNegativeFeedback(request, groupId, it.next().getId(),
true);
431 EntityManager em = AppBean.getPersistenceManager().getEM();
432 EntityTransaction transaction = em.getTransaction();
437 for (Iterator<Alternative> itAlt = alternatives.iterator(); itAlt.hasNext();) {
442 for (Iterator<AlternativeAttribute> itAttr = alt.
getAttributes().iterator(); itAttr.hasNext(); ) {
447 for (Iterator itFra = alt.
getFragments().iterator(); itFra.hasNext(); ) {
453 transaction.commit();
454 }
catch (Exception e) {
455 transaction.rollback();
457 String msg =
"Exception during the persisting of data.";
458 Logger.getLogger(AlternativeManager.class.getName()).log(Level.SEVERE, msg, e);
474 public static boolean overlappingFragment(ArrayList<SuggestionFragment> sugFragments,
int newFragStart,
int newFragEnd) {
475 Iterator<SuggestionFragment> sugFragmentsIt = sugFragments.iterator();
476 while(sugFragmentsIt.hasNext()){
480 if(actualSuggFragment == null){
485 int sugFragStart = actualSuggFragment.getOffset().intValue();
486 int sugFragEnd = sugFragStart + actualSuggFragment.getLength().intValue();
488 if(newFragStart <= sugFragStart && newFragEnd >= sugFragStart){
491 }
else if(newFragStart <= sugFragEnd && newFragEnd >= sugFragEnd){
506 ArrayList<Suggestion> sugAlternativeToPersist =
new ArrayList<Suggestion>();
508 for (Iterator<Alternative> altIt = alternatives.iterator(); altIt.hasNext(); ) {
510 sugAlternativeToPersist.add(
new Suggestion(alt));
513 return sugAlternativeToPersist;
520 Integer documentId = requestInfo.getSession().getSyncDocument().getId();
523 Object[] params = {
"sourceDocumentId",documentId};
524 @SuppressWarnings(
"unchecked")
525 List<Alternative> suggestionList = AppBean.getPersistenceManager().queryDB(
"Alternative.findBySourceDocumentID",params);
528 if (suggestionList != null && !suggestionList.isEmpty()) {
538 private static void setAsUsed(List<Alternative> alternatives) {
539 EntityManager em = AppBean.getPersistenceManager().getEM();
540 EntityTransaction transaction = em.getTransaction();
544 Iterator<Alternative> altIt = alternatives.iterator();
545 while (altIt.hasNext()) {
547 Object[] params = {
"id",alt.getId()};
548 @SuppressWarnings(
"unchecked")
549 List<Alternative> alternativeList = AppBean.getPersistenceManager().queryDB(
"Alternative.findById", params);
550 if (alternativeList == null || alternativeList.isEmpty()) {
552 String msg =
"Error during the setting of alternative usages (alternative was not found).";
553 Logger.getLogger(AlternativeManager.class.getName()).log(Level.SEVERE, msg);
562 transaction.commit();
563 }
catch (Exception e) {
564 transaction.rollback();
566 String msg =
"Exception during the setting of alternative usages.";
567 Logger.getLogger(AlternativeManager.class.getName()).log(Level.SEVERE, msg, e);
580 EntityManager em = AppBean.getPersistenceManager().getEM();
581 EntityTransaction transaction = em.getTransaction();
585 for (Iterator<Alternative> altIt = alternativeToPersist.iterator(); altIt.hasNext(); ) {
586 Integer altId = altIt.next().getId();
588 Query q = em.createNamedQuery(
"AlternativeAttribute.deleteByAltId");
589 q.setParameter(
"altId", altId);
592 q = em.createNamedQuery(
"AlternativeFragment.deleteByAltId");
593 q.setParameter(
"altId", altId);
596 q = em.createNamedQuery(
"Alternative.deleteById");
597 q.setParameter(
"id", altId);
601 transaction.commit();
602 }
catch (Exception e) {
603 transaction.rollback();
605 String msg =
"Exception during the deleting of data.";
606 Logger.getLogger(AlternativeManager.class.getName()).log(Level.SEVERE, msg, e);
619 EntityManager em = AppBean.getPersistenceManager().getEM();
620 EntityTransaction transaction = em.getTransaction();
624 for (Iterator<Suggestion> sugIt = confirmSuggs.iterator(); sugIt.hasNext(); ) {
628 Suggestion alternativeOf = sug.getAlternativeOfSuggestion();
629 if (alternativeOf == null) {
632 sugId = alternativeOf.getId();
635 Query q = em.createNamedQuery(
"Suggestion.clearIsFromSECAPI");
636 q.setParameter(
"id", sugId);
640 transaction.commit();
641 }
catch (Exception e) {
642 transaction.rollback();
644 String msg =
"Exception during the updating of data.";
645 Logger.getLogger(AlternativeManager.class.getName()).log(Level.SEVERE, msg, e);
659 Integer
id = alternativeOfId;
660 Object[] params = {
"alternativeOf",id.toString()};
661 @SuppressWarnings(
"unchecked")
662 List<Alternative> alternativeList = AppBean.getPersistenceManager().queryDB(
"Alternative.findByAlternativeOf",params);
663 if (alternativeList == null) {
664 alternativeList =
new ArrayList<Alternative>();
666 return alternativeList;
676 Integer
id = alternativeOfId;
677 Object[] params = {
"alternativeOf",id.toString()};
678 @SuppressWarnings(
"unchecked")
679 List<Suggestion> suggestionList = AppBean.getPersistenceManager().queryDB(
"Suggestion.findByAlternativeOf",params);
680 if (suggestionList == null) {
681 suggestionList =
new ArrayList<Suggestion>();
683 return suggestionList;
694 Integer
id = alternativeOfId;
695 Object[] params = {
"alternativeOf",id.toString(),
"notThisSugId", (Integer) notThisSugId};
696 @SuppressWarnings(
"unchecked")
697 List<Suggestion> suggestionList = AppBean.getPersistenceManager().queryDB(
"Suggestion.findByAlternativeOfAndNotSugg",params);
698 if (suggestionList == null) {
699 suggestionList =
new ArrayList<Suggestion>();
701 return suggestionList;
Class representing attribute of alternative of suggestion.
static LockMaster getLockMaster()
ArrayList< Suggestion > validSuggestions
void deleteAlternativesForDocument()
static boolean isSecCommandEmpty()
Class for manipulation with alternatives of suggestions.
void processAlternative()
static final int LOG_LEVEL_SERVER_INTERNAL_ERRORS
Singleton for storing global variables.
Class representing attribute of suggestion.
Class representing suggested annotation fragment.
Interface for call of SEC API as external program (deamon)
static List< Alternative > getAlternatives(int alternativeOfId)
Methods for translating output from SEC API to the Suggestion objects.
static void confirmSuggestions(RequestInfo request, ArrayList< Suggestion > confirmSuggs)
Class representing fragment for suggestion alternative.
Class provides offerining of suggestions with usage of local knowledge repository.
Static class which persists suggestion data to the database.
static void setAsUsed(List< Alternative > alternatives)
static final int LOG_LEVEL
Class representing type of annotation.
ArrayList< AnnotType > newTypes
AlternativeManager(RequestInfo request, String document, String documentUri, ArrayList< Suggestion > validSuggestions, SuggestionManager sugManager, Integer docID)
static List< Suggestion > getOtherSuggestions(int alternativeOfId)
EditorSession getSession()
Suggestion getAlternativeOfSuggestion()
static boolean overlappingFragment(ArrayList< SuggestionFragment > sugFragments, int newFragStart, int newFragEnd)
List< AlternativeAttribute > getAttributes()
static void clearIsFromSECAPI(ArrayList< Suggestion > confirmSuggs)
static void activateAlternatives(RequestInfo request, Suggestion sug)
Class representing alternative of suggestion.
static ArrayList< Suggestion > alternativesToSuggestions(ArrayList< Alternative > alternatives)
static final Boolean USE_REMOTE_SEC_API
static final int LOG_LEVEL_SERVER_ERRORS
SuggestionManager sugManager
Processed informations about client request.
Class representing suggestion of annotation.
void persistAlternatives(ArrayList< Alternative > alternatives)
static void refuseSuggestions(RequestInfo request, ArrayList< Suggestion > suggestionsToRefuse)
static boolean persistTypes(ArrayList< AnnotType > types)
static List< Suggestion > getOtherSuggestions(int alternativeOfId, int notThisSugId)
Informations about client session.
static void deleteAlternatives(List< Alternative > alternativeToPersist)