<commit_before>/**
 * Copyright (c) 2000-2009, Jasig, Inc.
 * See license distributed with this file and available online at
 * https://www.ja-sig.org/svn/jasig-parent/tags/rel-10/license-header.txt
 */
package org.jasig.portal.channel;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jasig.portal.AbstractChannelRegistryStore;
import org.jasig.portal.channel.dao.IChannelDefinitionDao;
import org.jasig.portal.channel.dao.IChannelTypeDao;
import org.jasig.portal.channel.dao.jpa.ChannelDefinitionImpl;
import org.jasig.portal.channel.dao.jpa.ChannelTypeImpl;
import org.jasig.portal.portlet.om.IPortletDefinition;
import org.jasig.portal.portlet.om.IPortletPreference;
import org.jasig.portal.portlet.om.IPortletPreferences;
import org.jasig.portal.portlet.registry.IPortletDefinitionRegistry;

/**
 * JpaChannelRegistryStore is a JPA/Hibernate implementation of the 
 * IChannelRegistryStore interface.  This implementation currently handles
 * IChannelDefinition and IChannelType persistence, while leaving GAP-based
 * category methods to the parent abstract class.
 * 
 * @author <NAME>, <EMAIL>
 * @revision $Revision$
 */
public final class JpaChannelRegistryStore extends AbstractChannelRegistryStore {
    
    private IChannelDefinitionDao channelDao;
    
    /**
     * Set the dao for channel definition persistence.
     * 
     * @param channelDao
     */
	public void setChannelDao(IChannelDefinitionDao channelDao) {
		this.channelDao = channelDao;
	}

    private IChannelTypeDao channelTypeDao;
    
    /**
     * Set the dao for channel type persistence.
     * 
     * @param channelTypeDao
     */
	public void setChannelTypeDao(IChannelTypeDao channelTypeDao) {
		this.channelTypeDao = channelTypeDao;
	}

	private IPortletDefinitionRegistry portletDefinitionRegistry;
    
	/**
	 * Set the portlet definition registry.
	 * 
	 * @param portletDefinitionRegistry
	 */
	public void setPortletDefinitionRegistry(
			IPortletDefinitionRegistry portletDefinitionRegistry) {
		this.portletDefinitionRegistry = portletDefinitionRegistry;
	}

	private Log log = LogFactory.getLog(JpaChannelRegistryStore.class);
    
	
    // Public ChannelDefinition methods

    /*
     * (non-Javadoc)
     * @see org.jasig.portal.IChannelRegistryStore#newChannelDefinition(int)
     */
    public IChannelDefinition newChannelDefinition(final int id) {
        return new ChannelDefinitionImpl(id);
    }

    /*
     * (non-Javadoc)
     * @see org.jasig.portal.IChannelRegistryStore#newChannelDefinition()
     */
    public IChannelDefinition newChannelDefinition() {
        return new ChannelDefinitionImpl();
    }

    /*
     * (non-Javadoc)
     * @see org.jasig.portal.IChannelRegistryStore#getChannelDefinition(int)
     */
    public IChannelDefinition getChannelDefinition(int channelId) {
    	IChannelDefinition def = channelDao.getChannelDefinition(channelId);
    	if (def != null) {
    		def = completeChannelDefinition(def);
    	}
    	return def;
    }

    /*
     * (non-Javadoc)
     * @see org.jasig.portal.IChannelRegistryStore#getChannelDefinition(java.lang.String)
     */
    public IChannelDefinition getChannelDefinition(String fname) {
    	IChannelDefinition def = channelDao.getChannelDefinition(fname);
    	if (def != null) {
    		def = completeChannelDefinition(def);
    	}
    	return def;
    }
    
    /*
     * (non-Javadoc)
     * @see org.jasig.portal.IChannelRegistryStore#saveChannelDefinition(org.jasig.portal.channel.IChannelDefinition)
     */
    public void saveChannelDefinition(IChannelDefinition channelDef) {
    	channelDef = channelDao.saveChannelDefinition(channelDef);
    	channelDef = getChannelDefinition(channelDef.getId());
    	
    	// if this channel is a portlet, save the associated portlet parameters
        if (channelDef.isPortlet()) {
            //Get or Create the portlet definition
            final IPortletDefinition portletDefinition = this.portletDefinitionRegistry.getOrCreatePortletDefinition(channelDef.getId());
            
            //Update the preferences of the portlet definition
            final IPortletPreferences portletPreferences = portletDefinition.getPortletPreferences();
            portletPreferences.setPortletPreferences(Arrays.asList(channelDef.getPortletPreferences()));
            this.portletDefinitionRegistry.updatePortletDefinition(portletDefinition);
        }
    }

    /*
     * (non-Javadoc)
     * @see org.jasig.portal.IChannelRegistryStore#deleteChannelDefinition(org.jasig.portal.channel.IChannelDefinition)
     */
    public void deleteChannelDefinition(IChannelDefinition channelDef) {
    	channelDao.deleteChannelDefinition(channelDef);
    }

    /*
     * (non-Javadoc)
     * @see org.jasig.portal.IChannelRegistryStore#getChannelDefinitions()
     */
    public List<IChannelDefinition> getChannelDefinitions() {
    	List<IChannelDefinition> defs = channelDao.getChannelDefinitions();
    	return defs;
    }
    
    
    // Public ChannelType methods
    
    /*<commit_msg>UP-2439 Updating channel definition persistence to properly save portlet preferences<commit_after>/**
 * Copyright (c) 2000-2009, Jasig, Inc.
 * See license distributed with this file and available online at
 * https://www.ja-sig.org/svn/jasig-parent/tags/rel-10/license-header.txt
 */
package org.jasig.portal.channel;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jasig.portal.AbstractChannelRegistryStore;
import org.jasig.portal.channel.dao.IChannelDefinitionDao;
import org.jasig.portal.channel.dao.IChannelTypeDao;
import org.jasig.portal.channel.dao.jpa.ChannelDefinitionImpl;
import org.jasig.portal.channel.dao.jpa.ChannelTypeImpl;
import org.jasig.portal.portlet.om.IPortletDefinition;
import org.jasig.portal.portlet.om.IPortletPreference;
import org.jasig.portal.portlet.om.IPortletPreferences;
import org.jasig.portal.portlet.registry.IPortletDefinitionRegistry;

/**
 * JpaChannelRegistryStore is a JPA/Hibernate implementation of the 
 * IChannelRegistryStore interface.  This implementation currently handles
 * IChannelDefinition and IChannelType persistence, while leaving GAP-based
 * category methods to the parent abstract class.
 * 
 * @author <NAME>, <EMAIL>
 * @revision $Revision$
 */
public final class JpaChannelRegistryStore extends AbstractChannelRegistryStore {
    
    private IChannelDefinitionDao channelDao;
    
    /**
     * Set the dao for channel definition persistence.
     * 
     * @param channelDao
     */
	public void setChannelDao(IChannelDefinitionDao channelDao) {
		this.channelDao = channelDao;
	}

    private IChannelTypeDao channelTypeDao;
    
    /**
     * Set the dao for channel type persistence.
     * 
     * @param channelTypeDao
     */
	public void setChannelTypeDao(IChannelTypeDao channelTypeDao) {
		this.channelTypeDao = channelTypeDao;
	}

	private IPortletDefinitionRegistry portletDefinitionRegistry;
    
	/**
	 * Set the portlet definition registry.
	 * 
	 * @param portletDefinitionRegistry
	 */
	public void setPortletDefinitionRegistry(
			IPortletDefinitionRegistry portletDefinitionRegistry) {
		this.portletDefinitionRegistry = portletDefinitionRegistry;
	}

	private Log log = LogFactory.getLog(JpaChannelRegistryStore.class);
    
	
    // Public ChannelDefinition methods

    /*
     * (non-Javadoc)
     * @see org.jasig.portal.IChannelRegistryStore#newChannelDefinition(int)
     */
    public IChannelDefinition newChannelDefinition(final int id) {
        return new ChannelDefinitionImpl(id);
    }

    /*
     * (non-Javadoc)
     * @see org.jasig.portal.IChannelRegistryStore#newChannelDefinition()
     */
    public IChannelDefinition newChannelDefinition() {
        return new ChannelDefinitionImpl();
    }

    /*
     * (non-Javadoc)
     * @see org.jasig.portal.IChannelRegistryStore#getChannelDefinition(int)
     */
    public IChannelDefinition getChannelDefinition(int channelId) {
    	IChannelDefinition def = channelDao.getChannelDefinition(channelId);
    	if (def != null) {
    		def = completeChannelDefinition(def);
    	}
    	return def;
    }

    /*
     * (non-Javadoc)
     * @see org.jasig.portal.IChannelRegistryStore#getChannelDefinition(java.lang.String)
     */
    public IChannelDefinition getChannelDefinition(String fname) {
    	IChannelDefinition def = channelDao.getChannelDefinition(fname);
    	if (def != null) {
    		def = completeChannelDefinition(def);
    	}
    	return def;
    }
    
    /*
     * (non-Javadoc)
     * @see org.jasig.portal.IChannelRegistryStore#saveChannelDefinition(org.jasig.portal.channel.IChannelDefinition)
     */
    public void saveChannelDefinition(IChannelDefinition channelDef) {
    	int channelId = channelDef.getId();
    	channelDao.saveChannelDefinition(channelDef);
    	if (channelId < 0) {
    		IChannelDefinition newChannel = getChannelDefinition(channelDef.getId());
    		channelId = newChannel.getId();
    	}
    	
    	// if this channel is a portlet, save the associated portlet parameters
        if (channelDef.isPortlet()) {
            //Get or Create the portlet definition
            final IPortletDefinition portletDefinition = this.portletDefinitionRegistry.getOrCreatePortletDefinition(channelId);
            
            //Update the preferences of the portlet definition
            final IPortletPreferences portletPreferences = portletDefinition.getPortletPreferences();
            portletPreferences.setPortletPreferences(Arrays.asList(channelDef.getPortletPreferences()));
            this.portletDefinitionRegistry.updatePortletDefinition(portletDefinition);
        }
    }

    /*
     * (non-Javadoc)
     * @see org.jasig.portal.IChannelRegistryStore#deleteChannelDefinition(org.jasig.portal.channel.IChannelDefinition)
     */
    public void deleteChannelDefinition(IChannelDefinition channelDef) {
    	channelDao.deleteChannelDefinition(channelDef);
    }

    /*
     * (non-Javadoc)
     * @see org.jasig.portal.IChannelRegistryStore#getChannelDefinitions()
     */
    public List<IChannelDefinition> getChannelDefinitions() {
    	List<IChannelDefinition> defs = channelDao.getChannelDefinitions();
    	return defs;
    }
    
    
    // Public ChannelType methods
    
    /*
{'fraction_non_alphanumeric': 0.0887522812409951, 'fraction_numerical': 0.0024013063106329844, 'mean_word_length': 3.5989399293286217, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 11, 'https://': 2, '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': '6819645', 'n_tokens_mistral': 2987, 'n_tokens_neox': 2987, 'n_words': 549}