<commit_before>/*
 * 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.navigation;

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

@Component("celements.celNavigationClasses")
public class NavigationClasses extends AbstractClassCollection {

  public static final String MENU_NAME_CLASS_DOC = "MenuName";
  public static final String MENU_NAME_CLASS_SPACE = "Celements2";
  public static final String MENU_NAME_CLASS = MENU_NAME_CLASS_SPACE + "."
      + MENU_NAME_CLASS_DOC;

  public static final String MAPPED_MENU_ITEM_CLASS_SPACE = "Classes";
  public static final String MAPPED_MENU_ITEM_CLASS_DOC = "MenuItemClass";
  public static final String MAPPED_MENU_ITEM_CLASS = MAPPED_MENU_ITEM_CLASS_SPACE + "."
        + MAPPED_MENU_ITEM_CLASS_DOC;

  public static final String NAVIGATION_CONFIG_CLASS_DOC = "NavigationConfigClass";
  public static final String NAVIGATION_CONFIG_CLASS_SPACE = MENU_NAME_CLASS_SPACE;
  public static final String NAVIGATION_CONFIG_CLASS = NAVIGATION_CONFIG_CLASS_SPACE
        + "." + NAVIGATION_CONFIG_CLASS_DOC;

  public static final String MENU_ELEMENT_NAME_FIELD = "menu_element_name";
  public static final String FROM_HIERARCHY_LEVEL_FIELD = "from_hierarchy_level";
  public static final String TO_HIERARCHY_LEVEL_FIELD = "to_hierarchy_level";
  public static final String SHOW_INACTIVE_TO_LEVEL_FIELD = "show_inactive_to_level";
  public static final String MENU_SPACE_FIELD = "menu_space";
  public static final String MENU_PART_FIELD = "menu_part";
  public static final String CM_CSS_CLASS_FIELD = "cm_css_class";
  public static final String LAYOUT_TYPE_FIELD = "layout_type";
  public static final String PRESENTATION_TYPE_FIELD = "presentation_type";

  public static final String MENU_ITEM_CLASS_DOC = "MenuItem";
  public static final String MENU_ITEM_CLASS_SPACE = MENU_NAME_CLASS_SPACE;
  public static final String MENU_ITEM_CLASS = MENU_ITEM_CLASS_SPACE + "."
        + MENU_ITEM_CLASS_DOC;

  private static Log LOGGER = LogFactory.getFactory().getInstance(
      NavigationClasses.class);

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

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

  @Override
  protected void initClasses() throws XWikiException {
    getMenuItemClass();
    getNewMenuItemClass();
    getMenuNameClass();
    getNavigationConfigClass();
  }

  public DocumentReference getMenuNameClassRef(String wikiName) {
    return new DocumentReference(wikiName, MENU_NAME_CLASS_SPACE, MENU_NAME_CLASS_DOC);
  }

  BaseClass getMenuNameClass() throws XWikiException {
    XWikiDocument doc;
    boolean needsUpdate = false;
    DocumentReference classRef = getMenuNameClassRef(getContext().getDatabase());

    try {
      doc = getContext().getWiki().getDocument(classRef, getContext());
    } catch (XWikiException exp) {
      LOGGER.error("Failed to get " + MENU_NAME_CLASS + " class document.", exp);
      doc = new XWikiDocument(classRef);
      needsUpdate = true;
    }

    BaseClass bclass = doc.getXClass();
    bclass.setDocumentReference(classRef);
    needsUpdate |= bclass.addTextField("menu_name", "Multilingual MenuName", 30);
    needsUpdate |= bclass.addTextField("lang", "Language", 5);
    needsUpdate |= bclass.addTextField("tooltip", "Tool Tip", 30);
    needsUpdate |= bclass.addTextField("image", "Background Image", 30);

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

    setContentAndSaveClassDocument(doc, needsUpdate);
    return bclass;
  }

  public DocumentReference getNavigationConfigClassRef(String wikiName) {
    return new DocumentReference(wikiName, NAVIGATION_CONFIG_CLASS_SPACE,
        NAVIGATION_CONFIG_CLASS_DOC);
  }

  BaseClass getNavigationConfigClass() throws XWikiException {
    XWikiDocument doc;
    boolean needsUpdate = false;<commit_msg>add constant for menu name fields<commit_after>/*
 * 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.navigation;

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

@Component("celements.celNavigationClasses")
public class NavigationClasses extends AbstractClassCollection {

  public static final String MENU_NAME_CLASS_DOC = "MenuName";
  public static final String MENU_NAME_CLASS_SPACE = "Celements2";
  public static final String MENU_NAME_CLASS = MENU_NAME_CLASS_SPACE + "."
      + MENU_NAME_CLASS_DOC;
  public static final String MENU_NAME_FIELD = "menu_name";
  public static final String MENU_NAME_IMAGE_FIELD = "image";
  public static final String MENU_NAME_TOOLTIP_FIELD = "tooltip";
  public static final String MENU_NAME_LANG_FIELD = "lang";

  public static final String MAPPED_MENU_ITEM_CLASS_SPACE = "Classes";
  public static final String MAPPED_MENU_ITEM_CLASS_DOC = "MenuItemClass";
  public static final String MAPPED_MENU_ITEM_CLASS = MAPPED_MENU_ITEM_CLASS_SPACE + "."
        + MAPPED_MENU_ITEM_CLASS_DOC;

  public static final String NAVIGATION_CONFIG_CLASS_DOC = "NavigationConfigClass";
  public static final String NAVIGATION_CONFIG_CLASS_SPACE = MENU_NAME_CLASS_SPACE;
  public static final String NAVIGATION_CONFIG_CLASS = NAVIGATION_CONFIG_CLASS_SPACE
        + "." + NAVIGATION_CONFIG_CLASS_DOC;

  public static final String MENU_ELEMENT_NAME_FIELD = "menu_element_name";
  public static final String FROM_HIERARCHY_LEVEL_FIELD = "from_hierarchy_level";
  public static final String TO_HIERARCHY_LEVEL_FIELD = "to_hierarchy_level";
  public static final String SHOW_INACTIVE_TO_LEVEL_FIELD = "show_inactive_to_level";
  public static final String MENU_SPACE_FIELD = "menu_space";
  public static final String MENU_PART_FIELD = "menu_part";
  public static final String CM_CSS_CLASS_FIELD = "cm_css_class";
  public static final String LAYOUT_TYPE_FIELD = "layout_type";
  public static final String PRESENTATION_TYPE_FIELD = "presentation_type";

  public static final String MENU_ITEM_CLASS_DOC = "MenuItem";
  public static final String MENU_ITEM_CLASS_SPACE = MENU_NAME_CLASS_SPACE;
  public static final String MENU_ITEM_CLASS = MENU_ITEM_CLASS_SPACE + "."
        + MENU_ITEM_CLASS_DOC;

  private static Log LOGGER = LogFactory.getFactory().getInstance(
      NavigationClasses.class);

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

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

  @Override
  protected void initClasses() throws XWikiException {
    getMenuItemClass();
    getNewMenuItemClass();
    getMenuNameClass();
    getNavigationConfigClass();
  }

  public DocumentReference getMenuNameClassRef(String wikiName) {
    return new DocumentReference(wikiName, MENU_NAME_CLASS_SPACE, MENU_NAME_CLASS_DOC);
  }

  BaseClass getMenuNameClass() throws XWikiException {
    XWikiDocument doc;
    boolean needsUpdate = false;
    DocumentReference classRef = getMenuNameClassRef(getContext().getDatabase());

    try {
      doc = getContext().getWiki().getDocument(classRef, getContext());
    } catch (XWikiException exp) {
      LOGGER.error("Failed to get " + MENU_NAME_CLASS + " class document.", exp);
      doc = new XWikiDocument(classRef);
      needsUpdate = true;
    }

    BaseClass bclass = doc.getXClass();
    bclass.setDocumentReference(classRef);
    needsUpdate |= bclass.addTextField(MENU_NAME_FIELD, "Multilingual MenuName", 30);
    needsUpdate |= bclass.addTextField(MENU_NAME_LANG_FIELD, "Language", 5);
    needsUpdate |= bclass.addTextField(MENU_NAME_TOOLTIP_FIELD, "Tool Tip", 30);
    needsUpdate |= bclass.addTextField(MENU_NAME_IMAGE_FIELD, "Background Image", 30);

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

    setContentAndSaveClassDocument(doc, needsUpdate);
    return bclass;
  }

  public DocumentReference getNavigationConfigClassRef(String wikiName) {
    return new DocumentReference(wikiName, NAVIGATION_CONFIG_CLASS_SPACE,
        NAVIGATION_CONFIG_CLASS_DOC);
  }

  BaseClass getNavigationConfigClass() throws XWikiException {
    XWikiDocument doc;
    boolean needsUpdate = false;
{'fraction_non_alphanumeric': 0.07654320987654321, 'fraction_numerical': 0.003988603988603989, 'mean_word_length': 4.9363021420518605, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5394666', 'n_tokens_mistral': 3186, 'n_tokens_neox': 3056, 'n_words': 846}