/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package de.spektrumprojekt.i.ranker.chain;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import de.spektrumprojekt.commons.chain.Command;
import de.spektrumprojekt.datamodel.common.Property;
import de.spektrumprojekt.datamodel.message.InteractionLevel;
import de.spektrumprojekt.datamodel.message.Message;
import de.spektrumprojekt.datamodel.message.MessageRank;
import de.spektrumprojekt.helper.MessageHelper;
import de.spektrumprojekt.i.ranker.UserSpecificMessageFeatureContext;
import de.spektrumprojekt.persistence.Persistence;

/**
 * A command to store the message ranks
 * 
 * @author Communote GmbH - <a href="http://www.communote.de/">http://www.communote.com/</a>
 * 
 */
public class UpdateInteractionLevelOfMessageRanksCommand implements
        Command<UserSpecificMessageFeatureContext> {

    private final Persistence persistence;

    /**
     * 
     * @param persistence
     *            the persistence to use
     */
    public UpdateInteractionLevelOfMessageRanksCommand(Persistence persistence) {
        if (persistence == null) {
            throw new IllegalArgumentException("persistence cannot be null");
        }
        this.persistence = persistence;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getConfigurationDescription() {
        return this.getClass().getSimpleName();
    }

    /**
     * 
     * @param message
     *            the message to get the parents of.
     * @return the message that are in the direct reply structure of the message. the root message
     *         will be first one. the message itself is not included
     */
    private List<Message> getParentMessages(Message message) {
        List<Message> parents = new ArrayList<Message>();
        Message parentMessage = message;
        while (parentMessage != null) {
            Property parentProp = MessageHelper.getParentMessage(parentMessage);
            if (parentProp != null) {
                String parentMessageId = parentProp.getPropertyValue();
                parentMessage = this.persistence.getMessageByGlobalId(parentMessageId);
                if (parentMessage != null) {
                    if (parents.contains(parentMessage)) {
                        throw new IllegalStateException(parentMessageId
                                + " is already in parents list. " + parents);
                    }
                    parents.add(parentMessage);
                }
            } else {
                parentMessage = null;
            }
        }
        Collections.reverse(parents);
        return parents;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void process(UserSpecificMessageFeatureContext context) {
        if (context.isNoRankingOnlyLearning()) {
            return;
        }

        List<Message> parentMessages = getParentMessages(context.getMessage());

        MessageRank messageRank = context.getMessageRank();
        if (messageRank != null) {
            if (messageRank.isAuthor()) {
                for (Message message : parentMessages) {
                    MessageRank parentRank = this.persistence.getMessageRank(
                            messageRank.getUserGlobalId(), message.getGlobalId());
                    if (parentRank != null) {
                        parentRank.setInteractionLevel(InteractionLevel.DIRECT);
                        context.addRankToUpdate(parentRank);
                    }
                }
            }
        }
    }
}

@@ -1348,24 +1348,84 @@
 reContext;%0D%0A
+import de.spektrumprojekt.i.ranker.chain.features.Feature;%0D%0A
 import de.sp
@@ -3887,144 +3887,59 @@
     
-MessageRank messageRank = context.getMessageRank();%0D%0A        if (messageRank != null) %7B%0D%0A            if (messageRank.isAuthor(
+if (context.check(Feature.AUTHOR_FEATURE, 1
 )) %7B%0D%0A
-    
+%0D%0A
     
@@ -3984,28 +3984,24 @@
 essages) %7B%0D%0A
-    
             
@@ -4091,23 +4091,15 @@
     
-    messageRank
+context
 .get
@@ -4147,36 +4147,32 @@
                 
-    
 if (parentRank !
@@ -4198,28 +4198,24 @@
             
- 
-   
 parentRank.s
@@ -4272,36 +4272,32 @@
                 
-   
- 
 context.addRankT
@@ -4318,39 +4318,16 @@
 Rank);%0D%0A
-                    %7D%0D%0A
         
@@ -4344,32 +4344,32 @@
 %0A            %7D%0D%0A
-
         %7D%0D%0A    %7D
@@ -4359,22 +4359,24 @@
 %0A        %7D%0D%0A
+%0D%0A
     %7D%0D%0A%7D%0D%0A

Use Feature Not Rank to evaluate