package com.celements.metatag;

import java.util.ArrayList;
import java.util.List;

import org.xwiki.component.annotation.Component;
import org.xwiki.component.annotation.Requirement;

import com.celements.model.context.ModelContext;

@Component
public class MetaTagService implements MetaTagSerivceRole {

  @Requirement
  ModelContext modelContext;

  @Override
  public void addMetaTagToCollector(MetaTag tag) {
    Object contextObj = modelContext.getXWikiContext().get(MetaTagSerivceRole.META_CONTEXT_KEY);
    List<MetaTag> metaList = getMetaTags(contextObj);
    if (metaList == null) {
      metaList = new ArrayList<>();
      modelContext.getXWikiContext().put(MetaTagSerivceRole.META_CONTEXT_KEY, metaList);
    }
    metaList.add(tag);
  }

  @Override
  public String displayCollectedMetaTags() {
    StringBuilder sb = new StringBuilder();
    Object contextObj = modelContext.getXWikiContext().remove(
        MetaTagSerivceRole.META_CONTEXT_KEY);
    List<MetaTag> metaTags = getMetaTags(contextObj);
    if (metaTags != null) {
      for (MetaTag metaTag : metaTags) {
        sb.append(metaTag.display()).append("\n");
      }
    }
    return sb.toString();
  }

  @SuppressWarnings("unchecked")
  List<MetaTag> getMetaTags(Object contextObj) {
    if (contextObj instanceof List) {
      return (List<MetaTag>) contextObj;
    }
    return null;
  }

}

@@ -181,49 +181,78 @@
 nt;%0A
-%0A
 import 
-com.celements.model.context.Model
+org.xwiki.context.Execution;%0Aimport org.xwiki.context.Execution
 Cont
@@ -350,33 +350,35 @@
 t%0A  
-ModelContext modelContext
+private Execution execution
 ;%0A%0A 
@@ -463,37 +463,28 @@
 xtObj = 
-modelContext.getXWiki
+getExecution
 Context(
@@ -488,16 +488,24 @@
 xt().get
+Property
 (MetaTag
@@ -663,42 +663,41 @@
     
-modelContext.getXWikiContext().put
+getExecutionContext().setProperty
 (Met
@@ -904,29 +904,96 @@
 j = 
-model
+getExecution
 Context
+()
 .get
-XWiki
+Property(MetaTagSerivceRole.META_CONTEXT_KEY);%0A    getExecution
 Cont
@@ -1008,18 +1008,17 @@
 move
-(%0A        
+Property(
 Meta
@@ -1433,24 +1433,24 @@
 tObj;%0A    %7D%0A
-
     return n
@@ -1459,10 +1459,101 @@
 l;%0A  %7D%0A%0A
+  private ExecutionContext getExecutionContext() %7B%0A    return execution.getContext();%0A  %7D%0A%0A
 %7D%0A

change collector to use ExecutionContext instead of XWikiContext