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

import org.apache.commons.lang.StringUtils;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.annotation.Requirement;
import org.xwiki.model.EntityType;
import org.xwiki.model.ModelConfiguration;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.model.reference.EntityReferenceResolver;
import org.xwiki.model.reference.InvalidEntityReferenceException;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Resolve an {@link EntityReference} into a valid and absolute reference (with all required parents filled in).
 * This implementation uses fixed default values when parts of the Reference are missing.
 *
 * @version $Id$
 * @since 2.2M1
 */
@Component("default/reference")
public class DefaultReferenceEntityReferenceResolver implements EntityReferenceResolver<EntityReference>
{
    @Requirement
    private ModelConfiguration configuration;

    private Map<EntityType, List<EntityType>> nextAllowedEntityTypes = new HashMap<EntityType, List<EntityType>>() {{
        put(EntityType.ATTACHMENT, Arrays.asList(EntityType.DOCUMENT));
        put(EntityType.DOCUMENT, Arrays.asList(EntityType.SPACE));
        put(EntityType.SPACE, Arrays.asList(EntityType.WIKI, EntityType.SPACE));
        put(EntityType.WIKI, Collections.<EntityType>emptyList());
    }};

    /**
     * {@inheritDoc}
     * @see org.xwiki.model.reference.EntityReferenceResolver#resolve(Object, org.xwiki.model.EntityType)
     * @throws InvalidEntityReferenceException if the passed reference to normalize is invalid (for example if the
     *         parent references are out of order)
     */
    public EntityReference resolve(EntityReference referenceToResolve, EntityType type)
    {
        EntityReference normalizedReference;

        // If the passed type is a supertype of the reference to resolve's type then we need to insert a top level
        // reference.
        if (type.ordinal() > referenceToResolve.getType().ordinal()) {
            normalizedReference = new EntityReference(getDefaultReferenceName(type), type, referenceToResolve.clone());
        } else {
            normalizedReference = referenceToResolve.clone();
        }

        // Check all references and parent references which have a NULL name and replace them with default values.
        // In addition insert references where needed.
        EntityReference reference = normalizedReference;
        while (reference != null) {
            List<EntityType> types = this.nextAllowedEntityTypes.get(reference.getType());
            if (reference.getParent() != null && !types.isEmpty() && !types.contains(reference.getParent().getType())) {
                // The parent reference isn't the allowed parent: insert an allowed reference
                EntityReference newReference = new EntityReference(
                    getDefaultReferenceName(types.get(0)), types.get(0), reference.getParent());
                reference.setParent(newReference);
            } else if (reference.getParent() == null && !types.isEmpty()) {
                // The top reference isn't the allowed top level reference, add a parent reference
                EntityReference newReference = new EntityReference(
                    getDefaultReferenceName(types.get(0)), types.get(0));
                reference.setParent(newReference);
            } else if (reference.getParent() != null && types.isEmpty()) {
                // There's a parent but not of the correct type... it means the reference is invalid
                throw new InvalidEntityReferenceException("Invalid reference [" + referenceToResolve + "]");
            }
            reference = reference.getParent();
        }

        // If the passed type is a subtype of the reference to resolve's type then we extract the reference.
        if (type.ordinal() < referenceToResolve.getType().ordinal()) {
            normalizedReference = normalizedReference.extractReference(type);
        }

        return normalizedReference;
    }

    protected String getDefaultReferenceName(EntityType type)
    {
        return this.configuration.getDefaultReferenceName(type);
    }
}

@@ -943,44 +943,127 @@
 ort 
-org.apache.commons.lang.StringUtils;
+java.util.Arrays;%0Aimport java.util.Collections;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0A
 %0Aimp
@@ -1416,135 +1416,8 @@
 n;%0A%0A
-import java.util.Arrays;%0Aimport java.util.Collections;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0A
 /**%0A
@@ -1528,24 +1528,24 @@
 led in).
-%0A *
  This
+%0A *
  impleme
@@ -1617,24 +1617,25 @@
  missing.%0A *
+ 
 %0A * @version
@@ -1981,12 +1981,29 @@
 %3E%3E()
- %7B%7B%0A
+%0A    %7B%0A        %7B%0A    
     
@@ -2070,32 +2070,36 @@
 MENT));%0A        
+ 
+   
 put(EntityType.D
@@ -2133,32 +2133,36 @@
 tyType.SPACE));%0A
+    
         put(Enti
@@ -2218,32 +2218,36 @@
 tyType.SPACE));%0A
+    
         put(Enti
@@ -2283,16 +2283,17 @@
 ityType%3E
+ 
 emptyLis
@@ -2302,17 +2302,26 @@
 ));%0A    
-%7D
+    %7D%0A    
 %7D;%0A%0A    
@@ -2345,16 +2345,24 @@
 ritDoc%7D%0A
+     * %0A
      * @
@@ -2589,16 +2589,20 @@
         
+    
 parent r
@@ -3810,37 +3810,16 @@
 erence =
- new EntityReference(
 %0A       
@@ -3823,32 +3823,56 @@
                 
+    new EntityReference(
 getDefaultRefere
@@ -4225,37 +4225,16 @@
 ference(
-%0A                    
 getDefau

Apply codestyle