<commit_before>/*
 * Copyright 2006-2008 The Kuali Foundation
 * 
 * Licensed under the Educational Community 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.opensource.org/licenses/ecl1.php
 * 
 * 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 org.kuali.kra.award.awardhierarchy;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.kuali.kra.award.AwardForm;
import org.kuali.kra.award.home.Award;
import org.kuali.kra.infrastructure.KraServiceLocator;
import org.kuali.rice.kns.util.MessageList;
import org.kuali.rice.kns.util.GlobalVariables;
import org.kuali.rice.kns.util.ErrorMessage;

import java.io.Serializable;
import java.util.*;

/**
 * Assumptions:
 *
 * 1) All hierarchy changes occur in memory only until an explicit save occurs
 *    a) Multiple hierarchy changes must be tracked until the save
 *    b) Many hierarchy operations require creation of Award copies or new Awards. To keep track of these new Awards, the hierarchy node should keep a
 *       transient reference to the new Award
 *
 * 2) Placeholder AwardDocument
 *    a) All Awards in all hierarchies will be referenced in a flattened list of awards on a placeholder AwardDocument
 *    b) This document will be used to save Awards. (Can BOS be used to handle only new Awards which is all AwardHierarchyService produces?)
 *
 * 3) Service methods will generally return a hierarchy node representing the results of an operation
 *    a) Hierarchy operations start with the awardNumber for the UI-selected hierarchy node
 *
 * 4) This bean will
 *    a) This bean will keep a reference to that placeholder document once obtained
 *    b) Keep the root node of all hierarchy roots (including the current hierarchy) produced during a user session
 *    c) Be used by AwardActionsAction (and AwardActions) to interact with hierarchy
 *
 * 5) On save,
 *    a) All hierarchy roots will be recursed to produce a flattened list of new hierarchy nodes. These will be saved with the BOS
 *    b) No deletes? No updates? (i.e. no move hierarchy?)
 *
 */
public class AwardHierarchyBean implements Serializable {
    private static final Log LOG = LogFactory.getLog(AwardHierarchyBean.class);
    
    private AwardForm awardForm;
    private transient AwardHierarchyService awardHierarchyService;

    Map<String, AwardHierarchy> rootNodes;
    private static final String ERROR_AWARD_HIERARCHY_NOTSAVED = "error.award.hierarchy.notsaved";

    /**
     * C'tor
     * @param awardForm
     */
    public AwardHierarchyBean(AwardForm awardForm) {
        this.awardForm = awardForm;
        init();
    }

    /**
     * C'tor mostly for testing
     * @param awardForm
     * @param awardHierarchyService
     */
    AwardHierarchyBean(AwardForm awardForm, AwardHierarchyService awardHierarchyService) {
        this.awardForm = awardForm;
        this.awardHierarchyService = awardHierarchyService;
        init();
    }

    public AwardHierarchy copyAwardAndDescendantsAsChildOfAnAwardInCurrentHierarchy(String sourceAwardNumber, String targetParentAwardNumber) {
        AwardHierarchy sourceNode = getRootNode().findNodeInHierarchy(sourceAwardNumber);
        AwardHierarchy targetParentNode = getRootNode().findNodeInHierarchy(targetParentAwardNumber);
        AwardHierarchy newNode = awardHierarchyService.copyAwardAndDescendantsAsChildOfAnAwardInCurrentHierarchy(sourceNode, targetParentNode);
        return newNode;
    }

    public AwardHierarchy copyAwardAndDescendantsAsChildOfAnAwardInAnotherHierarchy(String sourceAwardNumber, String targetParentAwardNumber) {
        AwardHierarchy sourceNode = getRootNode().findNodeInHierarchy(sourceAwardNumber);
        AwardHierarchy foreignRoot = awardHierarchyService.loadFullHierarchyFromAnyNode(targetParentAwardNumber);
        AwardHierarchy targetParentNode = foreignRoot.findNodeInHierarchy(targetParentAwardNumber);
        AwardHierarchy newBranchNode = awardHierarchyService.copyAwardAndDescendantsAsChildOfAnAwardInAnotherHierarchy(sourceNode, targetParentNode);
        rootNodes.put(foreignRoot.getAwardNumber(), foreignRoot);
        saveHierarchyChanges();
        return newBranchNode;
    }

    public AwardHierarchy copyAwardAsChildOfAnAwardInCurrentHierarchy(String sourceAwardNumber, String targetParentAwardNumber) {
        AwardHierarchy sourceNode = getRootNode().findNodeInHierarchy(sourceAwardNumber);
        AwardHierarchy targetParentNode = sourceNode.findNodeInHierarchy(targetParentAwardNumber);
        return awardHierarchyService.copyAwardAsChildOfAnAwardInCurrentHierarchy(sourceNode, targetParentNode);
    }

    public AwardHierarchy copyAwardAsChildOfAnAwardInAnotherHierarchy(String sourceAwardNumber, String targetParentAwardNumber) {
        AwardHierarchy sourceNode = getRootNode().findNodeInHierarchy(sourceAwardNumber);
        AwardHierarchy foreignRoot = awardHierarchyService.loadFullHierarchyFromAnyNode(targetParentAwardNumber);
        AwardHierarchy targetParentNode = foreignRoot.findNodeInHierarchy(targetParentAwardNumber);
        AwardHierarchy newNode = awardHierarchyService.copyAwardAsChildOfAnAwardInAnotherHierarchy(sourceNode, targetParentNode);
        rootNodes.put(foreignRoot.getAwardNumber(), foreignRoot);
        saveHierarchyChanges();
        return newNode;
    }

    public AwardHierarchy copyAwardAndAllDescendantsAsNewHierarchy(String awardNumber) {
        AwardHierarchy newRoot = awardHierarchyService.copyAwardAndAllDescendantsAsNewHierarchy(getRootNode().findNodeInHierarchy(awardNumber));
        rootNodes.put(newRoot.getAwardNumber(), newRoot);<commit_msg>KCAWD-390: Added method on helper bean to copy an Award to either the current hierarchy or another hierarchy, depending on whether the target awardNumber can be found in current hiearchy.<commit_after>/*
 * Copyright 2006-2008 The Kuali Foundation
 * 
 * Licensed under the Educational Community 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.opensource.org/licenses/ecl1.php
 * 
 * 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 org.kuali.kra.award.awardhierarchy;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.kuali.kra.award.AwardForm;
import org.kuali.kra.award.home.Award;
import org.kuali.kra.infrastructure.KraServiceLocator;
import org.kuali.rice.kns.util.MessageList;
import org.kuali.rice.kns.util.GlobalVariables;
import org.kuali.rice.kns.util.ErrorMessage;

import java.io.Serializable;
import java.util.*;

/**
 * Assumptions:
 *
 * 1) All hierarchy changes occur in memory only until an explicit save occurs
 *    a) Multiple hierarchy changes must be tracked until the save
 *    b) Many hierarchy operations require creation of Award copies or new Awards. To keep track of these new Awards, the hierarchy node should keep a
 *       transient reference to the new Award
 *
 * 2) Placeholder AwardDocument
 *    a) All Awards in all hierarchies will be referenced in a flattened list of awards on a placeholder AwardDocument
 *    b) This document will be used to save Awards. (Can BOS be used to handle only new Awards which is all AwardHierarchyService produces?)
 *
 * 3) Service methods will generally return a hierarchy node representing the results of an operation
 *    a) Hierarchy operations start with the awardNumber for the UI-selected hierarchy node
 *
 * 4) This bean will
 *    a) This bean will keep a reference to that placeholder document once obtained
 *    b) Keep the root node of all hierarchy roots (including the current hierarchy) produced during a user session
 *    c) Be used by AwardActionsAction (and AwardActions) to interact with hierarchy
 *
 * 5) On save,
 *    a) All hierarchy roots will be recursed to produce a flattened list of new hierarchy nodes. These will be saved with the BOS
 *    b) No deletes? No updates? (i.e. no move hierarchy?)
 *
 */
public class AwardHierarchyBean implements Serializable {
    private static final Log LOG = LogFactory.getLog(AwardHierarchyBean.class);
    
    private AwardForm awardForm;
    private transient AwardHierarchyService awardHierarchyService;

    Map<String, AwardHierarchy> rootNodes;
    private static final String ERROR_AWARD_HIERARCHY_NOTSAVED = "error.award.hierarchy.notsaved";

    /**
     * C'tor
     * @param awardForm
     */
    public AwardHierarchyBean(AwardForm awardForm) {
        this.awardForm = awardForm;
        init();
    }

    /**
     * C'tor mostly for testing
     * @param awardForm
     * @param awardHierarchyService
     */
    AwardHierarchyBean(AwardForm awardForm, AwardHierarchyService awardHierarchyService) {
        this.awardForm = awardForm;
        this.awardHierarchyService = awardHierarchyService;
        init();
    }

    public AwardHierarchy copyAwardAndDescendantsAsChildOfAnAwardInCurrentHierarchy(String sourceAwardNumber, String targetParentAwardNumber) {
        AwardHierarchy sourceNode = getRootNode().findNodeInHierarchy(sourceAwardNumber);
        AwardHierarchy targetParentNode = getRootNode().findNodeInHierarchy(targetParentAwardNumber);
        AwardHierarchy newNode = awardHierarchyService.copyAwardAndDescendantsAsChildOfAnAwardInCurrentHierarchy(sourceNode, targetParentNode);
        return newNode;
    }

    public AwardHierarchy copyAwardAndDescendantsAsChildOfAnAwardInAnotherHierarchy(String sourceAwardNumber, String targetParentAwardNumber) {
        AwardHierarchy sourceNode = getRootNode().findNodeInHierarchy(sourceAwardNumber);
        AwardHierarchy foreignRoot = awardHierarchyService.loadFullHierarchyFromAnyNode(targetParentAwardNumber);
        AwardHierarchy targetParentNode = foreignRoot.findNodeInHierarchy(targetParentAwardNumber);
        AwardHierarchy newBranchNode = awardHierarchyService.copyAwardAndDescendantsAsChildOfAnAwardInAnotherHierarchy(sourceNode, targetParentNode);
        rootNodes.put(foreignRoot.getAwardNumber(), foreignRoot);
        saveHierarchyChanges();
        return newBranchNode;
    }

    public AwardHierarchy copyAwardAsChildOfAnotherAward(String sourceAwardNumber, String targetParentAwardNumber) {
        return getRootNode().findNodeInHierarchy(targetParentAwardNumber) != null ?
                        copyAwardAsChildOfAnAwardInCurrentHierarchy(sourceAwardNumber, targetParentAwardNumber) :
                        copyAwardAsChildOfAnAwardInAnotherHierarchy(sourceAwardNumber, targetParentAwardNumber);
    }

    public AwardHierarchy copyAwardAsChildOfAnAwardInCurrentHierarchy(String sourceAwardNumber, String targetParentAwardNumber) {
        AwardHierarchy sourceNode = getRootNode().findNodeInHierarchy(sourceAwardNumber);
        AwardHierarchy targetParentNode = sourceNode.findNodeInHierarchy(targetParentAwardNumber);
        return awardHierarchyService.copyAwardAsChildOfAnAwardInCurrentHierarchy(sourceNode, targetParentNode);
    }

    public AwardHierarchy copyAwardAsChildOfAnAwardInAnotherHierarchy(String sourceAwardNumber, String targetParentAwardNumber) {
        AwardHierarchy sourceNode = getRootNode().findNodeInHierarchy(sourceAwardNumber);
        AwardHierarchy foreignRoot = awardHierarchyService.loadFullHierarchyFromAnyNode(targetParentAwardNumber);
        AwardHierarchy targetParentNode = foreignRoot.findNodeInHierarchy(targetParentAwardNumber);
        AwardHierarchy newNode = awardHierarchyService.copyAwardAsChildOfAnAwardInAnotherHierarchy(sourceNode, targetParentNode);
        rootNodes.put(foreignRoot.getAwardNumber(), foreignRoot);
        saveHierarchyChanges();
        return newNode;
    }

    public AwardHierarchy copyAwardAndAllDescendantsAsNewHierarchy(String awardNumber) {
        AwardHierarchy newRoot = awardHierarchyService.copyAwardAndAllDescendantsAsNewHierarchy(getRootNode().findNodeInHierarchy(awardNumber));
        rootNodes.put(newRoot.getAwardNumber(), newRoot);
{'fraction_non_alphanumeric': 0.06159563532853641, 'fraction_numerical': 0.002767454732347592, 'mean_word_length': 4.6063829787234045, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '377324', 'n_tokens_mistral': 3533, 'n_tokens_neox': 3464, 'n_words': 957}