<commit_before>/**
 * $RCSfile: ,v $
 * $Revision: $
 * $Date: $
 * 
 * Copyright (C) 2004-2010 Jive Software. All rights reserved.
 *
 * Licensed 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 org.jivesoftware.sparkimpl.plugin.privacy.list;

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

import org.jivesoftware.smack.PrivacyList;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.PrivacyItem;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.spark.util.log.Log;
import org.jivesoftware.sparkimpl.plugin.privacy.PrivacyException;
import org.jivesoftware.sparkimpl.plugin.privacy.PrivacyManager;

/**
 *
 * @author <NAME>
 */
public abstract class SparkPrivacyList {

    /**
     * List name will be used to identify PrivacyList
     */
    protected String listName   = "";

    /**
     * List Items
     */
    protected ArrayList<PrivacyItem> privacyItems;

    /**
     * Listeners
     */
    private final List<SparkPrivacyListListener> listeners = new ArrayList<SparkPrivacyListListener>();

    /**
     * Action associated with the items, it MUST be filled and will allow or deny
     * the communication by default
     */
    protected boolean allowByDefault = true;

    
    /**
     * Set PrivacyList name
     * 
     * Only children can set listName
     * 
     * @param listName name of the PrivacyList
     */
    protected final void setListName(String listName) {
        this.listName = listName;
    }

    /**
     * Loads list from server.
     *
     * If list not exist we will try to create it.
     * 
     */
    protected final void loadList() throws XMPPException, PrivacyException {
        if ( listName.isEmpty() ) {
            throw new PrivacyException("List name not defined.");
        }
        try {
            if ( PrivacyManager.getInstance().getPrivacyListManager() == null ) {
                // @todo handle error
            }
            PrivacyList list = PrivacyManager.getInstance().getPrivacyListManager().getPrivacyList( getListName() );
            privacyItems = (ArrayList<PrivacyItem>) list.getItems();
            if ( privacyItems.size() > 0 ) { // Check for the default item
                // @todo
                PrivacyItem defaultItem = getDefaultPrivacyItem(); //Get Last Item (Default Item allways must be the last one.)
                if ( defaultItem != null ) { // Is this Item default ?
                    //Yep this item is default get its rule.
                    allowByDefault = defaultItem.isAllow();
                }
                privacyItems.remove(defaultItem); // Removing Default Item from array list, we will add it on next save.
            }
        } catch(XMPPException ex) {
            if ( ex.getXMPPError().getCode() == 404 ) { // List not found. Let's create it.
                createList(); // Creating new PrivacyList.
            }
            Log.error(ex); // Log all errors
        }
        
    }

    /**
     * Get maximal Order value from PrivacyItemList
     *
     * @return
     */
    protected int getMaxItemOrder() {
        return getLastItem().getOrder();
    }

    /**
     * Load default privecy item
     * 
     * @return
     */
    protected PrivacyItem getDefaultPrivacyItem() {
        PrivacyItem item = null;
        for (PrivacyItem privacyItem : getPrivacyItems()) {
            if ( privacyItem.getValue() == null && privacyItem.getType() == null ) {
                item = privacyItem;
            }
        }
        return item;
    }

    /**
     * Get last PrivacyItem from list ordered by PrivacyItem.order
     *
     * @return last PrivacyItem ordered by Item order
     */
    protected PrivacyItem getLastItem() {
        int order = 0;
        PrivacyItem item = null;
        for (PrivacyItem privacyItem : getPrivacyItems()) {
            if ( order < privacyItem.getOrder() ) {
                order = privacyItem.getOrder();
                item = privacyItem;<commit_msg>SPARK-1251: PrivacyPlugin causes NullPointerE. when clicking on block contact<commit_after>/**
 * $RCSfile: ,v $
 * $Revision: $
 * $Date: $
 * 
 * Copyright (C) 2004-2010 Jive Software. All rights reserved.
 *
 * Licensed 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 org.jivesoftware.sparkimpl.plugin.privacy.list;

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

import org.jivesoftware.smack.PrivacyList;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.PrivacyItem;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.spark.util.log.Log;
import org.jivesoftware.sparkimpl.plugin.privacy.PrivacyException;
import org.jivesoftware.sparkimpl.plugin.privacy.PrivacyManager;

/**
 *
 * @author <NAME>
 */
public abstract class SparkPrivacyList {

    /**
     * List name will be used to identify PrivacyList
     */
    protected String listName   = "";

    /**
     * List Items
     */
    protected ArrayList<PrivacyItem> privacyItems;

    /**
     * Listeners
     */
    private final List<SparkPrivacyListListener> listeners = new ArrayList<SparkPrivacyListListener>();

    /**
     * Action associated with the items, it MUST be filled and will allow or deny
     * the communication by default
     */
    protected boolean allowByDefault = true;

    
    /**
     * Set PrivacyList name
     * 
     * Only children can set listName
     * 
     * @param listName name of the PrivacyList
     */
    protected final void setListName(String listName) {
        this.listName = listName;
    }

    /**
     * Loads list from server.
     *
     * If list not exist we will try to create it.
     * 
     */
    protected final void loadList() throws XMPPException, PrivacyException {
        if ( listName.isEmpty() ) {
            throw new PrivacyException("List name not defined.");
        }
        try {
            if ( PrivacyManager.getInstance().getPrivacyListManager() == null ) {
                // @todo handle error
            }
            PrivacyList list = PrivacyManager.getInstance().getPrivacyListManager().getPrivacyList( getListName() );
            privacyItems = (ArrayList<PrivacyItem>) list.getItems();
            if ( privacyItems.size() > 0 ) { // Check for the default item
                // @todo
                PrivacyItem defaultItem = getDefaultPrivacyItem(); //Get Last Item (Default Item allways must be the last one.)
                if ( defaultItem != null ) { // Is this Item default ?
                    //Yep this item is default get its rule.
                    allowByDefault = defaultItem.isAllow();
                }
                privacyItems.remove(defaultItem); // Removing Default Item from array list, we will add it on next save.
            }
        } catch(XMPPException ex) {
            if ( ex.getXMPPError().getCode() == 404 ) { // List not found. Let's create it.
                createList(); // Creating new PrivacyList.
            }
            Log.error(ex); // Log all errors
        }
        
    }

    /**
     * Get maximal Order value from PrivacyItemList
     *
     * @return
     */
    protected int getMaxItemOrder() {
        if(getLastItem() != null) {
            return getLastItem().getOrder();
        }
        return 1;
    }

    /**
     * Load default privecy item
     * 
     * @return
     */
    protected PrivacyItem getDefaultPrivacyItem() {
        PrivacyItem item = null;
        for (PrivacyItem privacyItem : getPrivacyItems()) {
            if ( privacyItem.getValue() == null && privacyItem.getType() == null ) {
                item = privacyItem;
            }
        }
        return item;
    }

    /**
     * Get last PrivacyItem from list ordered by PrivacyItem.order
     *
     * @return last PrivacyItem ordered by Item order
     */
    protected PrivacyItem getLastItem() {
        int order = 0;
        PrivacyItem item = null;
        for (PrivacyItem privacyItem : getPrivacyItems()) {
            if ( order < privacyItem.getOrder() ) {
                order = privacyItem.getOrder();
                item = privacyItem;
{'fraction_non_alphanumeric': 0.09541566132923887, 'fraction_numerical': 0.004277253783724501, 'mean_word_length': 2.1773519163763067, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '2895700', 'n_tokens_mistral': 2501, 'n_tokens_neox': 2410, 'n_words': 751}