/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.celements.web.classcollections;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xwiki.component.annotation.Component;
import org.xwiki.model.reference.DocumentReference;

import com.celements.common.classes.CelementsClassCollection;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.classes.BaseClass;

@Component("celements.imageMap.classes")
public class ImageMapClasses extends CelementsClassCollection {
  
  private static Log mLogger = LogFactory.getFactory().getInstance(
      ImageMapClasses.class);

  public ImageMapClasses() { }
  
  @Override
  protected Log getLogger() {
    return mLogger;
  }

  @Override
  protected void initClasses(XWikiContext context) throws XWikiException {
    getImageMapConfigClass(context);
    getImageMapClass(context);
  }
  
  protected BaseClass getImageMapConfigClass(XWikiContext context) throws XWikiException {
    XWikiDocument doc;
    boolean needsUpdate = false;
    DocumentReference classRef = new DocumentReference(context.getDatabase(), "Classes",
      "ImageMapConfigClass");

    try {
      doc = context.getWiki().getDocument(classRef, context);
    } catch (XWikiException e) {
      mLogger.error(e);
      doc = new XWikiDocument(classRef);
      needsUpdate = true;
    }
    
    BaseClass bclass = doc.getXClass();
    bclass.setDocumentReference(classRef);
    needsUpdate |= bclass.addTextField("map_id", "Map Identifier", 30);
    needsUpdate |= bclass.addTextField("lang", "Language", 30);
    needsUpdate |= bclass.addTextAreaField("map", "Map Code", 80, 15);
    
    if(!"internal".equals(bclass.getCustomMapping())){
      needsUpdate = true;
      bclass.setCustomMapping("internal");
    }
    
    setContentAndSaveClassDocument(doc, needsUpdate, context);
    return bclass;
  }
  
  protected BaseClass getImageMapClass(XWikiContext context) throws XWikiException {
    XWikiDocument doc;
    boolean needsUpdate = false;
    DocumentReference classRef = new DocumentReference(context.getDatabase(), "Classes",
      "ImageMapClass");
    
    try {
      doc = context.getWiki().getDocument(classRef, context);
    } catch (XWikiException e) {
      mLogger.error(e);
      doc = new XWikiDocument(classRef);
      needsUpdate = true;
    }
    
    BaseClass bclass = doc.getXClass();
    bclass.setDocumentReference(classRef);
    needsUpdate |= bclass.addTextField("map_id", "Map Identifier", 30);
    
    setContentAndSaveClassDocument(doc, needsUpdate, context);
    return bclass;
  }

  public String getConfigName() {
    return "imageMap";
  }

}

@@ -1155,25 +1155,24 @@
 classes.
-Celements
+Abstract
 ClassCol
@@ -1184,43 +1184,8 @@
 on;%0A
-import com.xpn.xwiki.XWikiContext;%0A
 impo
@@ -1388,17 +1388,16 @@
 nds 
-Celements
+Abstract
 Clas
@@ -1434,23 +1434,22 @@
 tic Log 
-mLogger
+LOGGER
  = LogFa
@@ -1479,23 +1479,16 @@
 nstance(
-%0A      
 ImageMap
@@ -1591,23 +1591,22 @@
  return 
-mLogger
+LOGGER
 ;%0A  %7D%0A%0A 
@@ -1645,36 +1645,16 @@
 Classes(
-XWikiContext context
 ) throws
@@ -1694,31 +1694,24 @@
 ConfigClass(
-context
 );%0A    getIm
@@ -1722,23 +1722,16 @@
 apClass(
-context
 );%0A  %7D%0A 
@@ -1728,39 +1728,37 @@
 s();%0A  %7D%0A  %0A  pr
-otected
+ivate
  BaseClass getIm
@@ -1775,36 +1775,16 @@
 igClass(
-XWikiContext context
 ) throws
@@ -1904,39 +1904,44 @@
 cumentReference(
-c
+getC
 ontext
+()
 .getDatabase(), 
@@ -1931,32 +1931,40 @@
 ).getDatabase(),
+%0A       
  %22Classes%22,%0A    
@@ -1950,38 +1950,32 @@
       %22Classes%22,
-%0A     
  %22ImageMapConfig
@@ -1998,39 +1998,44 @@
 y %7B%0A      doc = 
-c
+getC
 ontext
+()
 .getWiki().getDo
@@ -2043,39 +2043,44 @@
 ument(classRef, 
-c
+getC
 ontext
+()
 );%0A    %7D catch (
@@ -2087,32 +2087,34 @@
 XWikiException e
+xp
 ) %7B%0A      mLogge
@@ -2103,39 +2103,88 @@
 xp) %7B%0A      
-mLogger.error(e
+LOGGER.error(%22Failed to get ImageMapConfigClass document. %22, exp
 );%0A      doc
@@ -2729,33 +2729,24 @@
  needsUpdate
-, context
 );%0A    retur
@@ -2805,28 +2805,8 @@
 ass(
-XWikiContext context
 ) th
@@ -2938,23 +2938,28 @@
 ference(
-c
+getC
 ontext
+()
 .getData
@@ -2965,16 +2965,24 @@
 abase(),
+%0A       
  %22Classe
@@ -2984,22 +2984,16 @@
 lasses%22,
-%0A     
  %22ImageM
@@ -3030,23 +3030,28 @@
   doc = 
-c
+getC
 ontext
+()
 .getWiki
@@ -3075,23 +3075,28 @@
 assRef, 
-c
+getC
 ontext
+()
 );%0A    %7D
@@ -3119,16 +3119,18 @@
 eption e
+xp
 ) %7B%0A    
@@ -3135,23 +3135,66 @@
     
-mLogger.error(e
+LOGGER.error(%22Failed to get ImageMapClass document. %22, exp
 );%0A 
@@ -3485,17 +3485,8 @@
 date
-, context
 );%0A 

update deprecated usages in ImageMapClasses