/*
 * 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.xwiki.model.reference.DocumentReference;

import com.xpn.xwiki.XWikiContext;

public class TreeNode {

  private String parent;
  
  private Integer position;

  private String partName;

  private IPartNameGetStrategy partNameGetStrategy;

  private DocumentReference docRef;

  private String databaseName;
  
  @Deprecated
  public TreeNode(String fullName, String parent, Integer position, String databaseName) {
    this.databaseName = databaseName;
    setFullName(fullName);
    setParent(parent);
    setPosition(position);
  }

  public TreeNode(DocumentReference docRef, String parent, Integer position) {
    setDocumentReference(docRef);
    setParent(parent);
    setPosition(position);
  }

  /**
   * 
   * @return fullName
   * 
   * @deprecated since 2.14.0 use getDocumentReference instead
   */
  @Deprecated
  public String getFullName() {
    return docRef.getLastSpaceReference().getName() + "." + docRef.getName();
  }

  void setDocumentReference(DocumentReference docRef) {
    this.docRef = docRef;
  }

  @Deprecated
  void setFullName(String fullName) {
    setDocumentReference(new DocumentReference(databaseName, fullName.split("\\.")[0],
        fullName.split("\\.")[1]));
  }

  public String getParent() {
    return parent;
  }

  void setParent(String parent) {
    if (parent == null) {
      parent = "";
    }
    this.parent = parent;
  }

  public Integer getPosition() {
    if (position == null) {
      position = 0;
    }
    return position;
  }

  void setPosition(Integer position) {
    this.position = position;
  }

  public String getPartName(XWikiContext context) {
    if (partName == null) {
      if (partNameGetStrategy != null) {
        partName = partNameGetStrategy.getPartName(getFullName(), context);
      } else {
        partName = "";
      }
    }
    return partName;
  }

  public void setPartName(String partName) {
    if (partName == null) {
        partName = "";
    }
    this.partName = partName;
  }

  public void setPartNameGetStrategy(IPartNameGetStrategy strategy) {
    this.partNameGetStrategy = strategy;
  }

  public DocumentReference getDocumentReference() {
    return docRef;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if ((obj == null) || (obj.getClass() != this.getClass())) {
      return false;
    }
    // object must be Test at this point
    TreeNode node = (TreeNode) obj;
    return docRef.equals(node.docRef) && (position == node.position);
  }

  @Override
  public int hashCode() {
    int hash = 7;
    hash = 31 * hash + (position == null ? 0 : position.hashCode());
    hash = 31 * hash + (docRef == null ? 0 : docRef.hashCode());
    return hash;
  }

}

@@ -922,16 +922,95 @@
 ation;%0A%0A
+import org.xwiki.context.Execution;%0Aimport org.xwiki.context.ExecutionContext;%0A
 import o
@@ -1088,16 +1088,48 @@
 Context;
+%0Aimport com.xpn.xwiki.web.Utils;
 %0A%0Apublic
@@ -3767,10 +3767,446 @@
 h;%0A  %7D%0A%0A
+  @Override%0A  public String toString() %7B%0A    return %22%5B docRef = %5B%22 + docRef + %22%5D, parent = %5B%22 + parent + %22%5D, position = %5B%22%0A    + position + %22%5D, partName = %5B%22 + getPartName(getContext()) + %22%5D %5D%22;%0A  %7D%0A%0A  private XWikiContext getContext() %7B%0A    return (XWikiContext)getExecutionContext().getProperty(%22xwikicontext%22);%0A  %7D%0A%0A  private ExecutionContext getExecutionContext() %7B%0A    return Utils.getComponent(Execution.class).getContext();%0A  %7D%0A%0A
 %7D%0A

add toString to TreeNode