/*
 * 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.AbstractClassCollection;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.classes.BaseClass;

@Component("celements.documentDetails")
public class DocumentDetailsClasses extends AbstractClassCollection {
  
  private static Log mLogger = LogFactory.getFactory().getInstance(
      DocumentDetailsClasses.class);

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

  @Override
  protected void initClasses() throws XWikiException {
    getDocumentPublicationClass();
    getDocumentExtractClass();
  }
  
  BaseClass getDocumentPublicationClass() throws XWikiException {
    XWikiDocument doc;
    boolean needsUpdate = false;
    DocumentReference classRef = new DocumentReference(getContext().getDatabase(),
        "Classes", "DocumentPublication");

    try {
      doc = getContext().getWiki().getDocument(classRef, getContext());
    } catch (XWikiException exp) {
      mLogger.error(exp);
      doc = new XWikiDocument(classRef);
      needsUpdate = true;
    }

    BaseClass bclass = doc.getXClass();
    bclass.setDocumentReference(classRef);
    needsUpdate |= bclass.addDateField("publishDate", "Publish Date", 
        "dd.MM.yyyy HH:mm:ss", 0);
    needsUpdate |= bclass.addDateField("unpublishDate", "Unpublish Date", 
        "dd.MM.yyyy HH:mm:ss", 0);

    if (!"internal".equals(bclass.getCustomMapping())) {
      needsUpdate = true;
      bclass.setCustomMapping("internal");
    }

    setContentAndSaveClassDocument(doc, needsUpdate);
    return bclass;
  }

  BaseClass getDocumentExtractClass() throws XWikiException {
    XWikiDocument doc;
    boolean needsUpdate = false;
    DocumentReference classRef = new DocumentReference(getContext().getDatabase(),
        "Classes", "DocumentExtract");

    try {
      doc = getContext().getWiki().getDocument(classRef, getContext());
    } catch (XWikiException exp) {
      mLogger.error(exp);
      doc = new XWikiDocument(classRef);
      needsUpdate = true;
    }

    BaseClass bclass = doc.getXClass();
    bclass.setDocumentReference(classRef);
    needsUpdate |= bclass.addTextField("language", "Language", 30);
    needsUpdate |= bclass.addTextAreaField("extract", "Extract", 80, 7);

    if (!"internal".equals(bclass.getCustomMapping())) {
      needsUpdate = true;
      bclass.setCustomMapping("internal");
    }

    setContentAndSaveClassDocument(doc, needsUpdate);
    return bclass;
  }


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

}

@@ -2340,32 +2340,51 @@
 %22, %22Publish Date
+ (dd.MM.yyyy HH:mm)
 %22, %0A        %22dd.
@@ -2396,23 +2396,20 @@
 yy HH:mm
-:ss
 %22, 
-0
+1
 );%0A    n
@@ -2474,27 +2474,50 @@
 ish Date
-%22, %0A       
+ (dd.MM.yyyy %22 +%0A        %22HH:mm)%22,
  %22dd.MM.
@@ -2530,11 +2530,8 @@
 H:mm
-:ss
 %22, 0

set date format to dd.MM.yyyy HH:mm