<commit_before>package org.unitedinternet.cosmo.dao.external;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.unitedinternet.cosmo.dao.ContentDao;
import org.unitedinternet.cosmo.ext.ContentSource;
import org.unitedinternet.cosmo.model.BaseEventStamp;
import org.unitedinternet.cosmo.model.CalendarCollectionStamp;
import org.unitedinternet.cosmo.model.CollectionItem;
import org.unitedinternet.cosmo.model.ContentItem;
import org.unitedinternet.cosmo.model.HomeCollectionItem;
import org.unitedinternet.cosmo.model.Item;
import org.unitedinternet.cosmo.model.NoteItem;
import org.unitedinternet.cosmo.model.Ticket;
import org.unitedinternet.cosmo.model.User;
import org.unitedinternet.cosmo.model.filter.ItemFilter;
import org.unitedinternet.cosmo.model.hibernate.HibNoteItem;

import com.google.common.base.Strings;

/**
 * <code>ContentDao</code> that fetches calendar content from external providers.
 * 
 * @author <NAME>
 *
 */
public class ContentDaoExternal implements ContentDao {

    private static final Log LOG = LogFactory.getLog(ContentDaoExternal.class);

    private final ContentSource contentSource;

    private final ContentDao contentDaoInternal;

    public ContentDaoExternal(ContentSource contentSource, final ContentDao contentDaoInternal) {
        this.contentSource = contentSource;
        this.contentDaoInternal = contentDaoInternal;
    }

    @Override
    public Item findItemByPath(String path) {
        PathSegments extPath = new PathSegments(path);
        
        String homeCollectionUid = validHomeCollectionUidFrom(extPath);
        
        String collectionUid = validCollectionUidFrom(extPath);
        
        Item collectionItem = this.contentDaoInternal.findItemByPath(homeCollectionUid + "/" + collectionUid);
        
        if (collectionItem == null) {
            throw new IllegalArgumentException("Could not find collection for path: " + homeCollectionUid + "/" + collectionUid);
        }
        
        CalendarCollectionStamp stamp = (CalendarCollectionStamp) collectionItem.getStamp(CalendarCollectionStamp.class);
        
        if(stamp == null){
            throw new IllegalStateException("Found calendar without stamp for path [" + path + "]");
        }
        
        String targetUri = stamp.getTargetUri();
        
        Set<NoteItem> itemsFromUri = this.contentSource.getContent(targetUri);
        
        for(NoteItem item : itemsFromUri){
            item.setOwner(collectionItem.getOwner());
        }
        
        String eventUid = extPath.getEventUid();
        
        if(Strings.isNullOrEmpty(eventUid)){
            return new ExternalCollectionItem((CollectionItem)collectionItem, itemsFromUri);
        }
        for(NoteItem noteItem : itemsFromUri){
            if(eventUid.equals(noteItem.getName())){
                return noteItem;
            }
        }
        return null;
    }

    private static String validCollectionUidFrom(PathSegments extPath) {
        String collectionUid = extPath.getCollectionUid();
        
        if (collectionUid == null || collectionUid.trim().isEmpty()) {
            throw new IllegalArgumentException("Collection path cannot be null or empty.");
        }
        return collectionUid;
    }

    private static String validHomeCollectionUidFrom(PathSegments extPath) {
        String homeCollectionUid = extPath.getHomeCollectionUid();
        
        if (homeCollectionUid == null || homeCollectionUid.trim().isEmpty()) {
            throw new IllegalArgumentException("Home path path cannot be null or empty.");
        }
        return homeCollectionUid;
    }

    @Override
    public Item findItemByPath(String path, String parentUid) {
        LOG.info("EXTERNAL Delegating call to internal for parentUid: " + parentUid + " and path: " + path);
        return this.contentDaoInternal.findItemByPath(path, parentUid);
    }

    @Override
    public Set<Item> findItems(ItemFilter filter) {
        Set<Item> items = new HashSet<>();
        if (filter != null && filter.getParent() != null) {
            CollectionItem calendarItem = filter.getParent();
            CalendarCollectionStamp stamp = (CalendarCollectionStamp) calendarItem.getStamp(CalendarCollectionStamp.class);
            if (stamp != null) {
                String targetUri = stamp.getTargetUri();
                Set<NoteItem> noteItems = this.contentSource.getContent(targetUri);
                this.postProcess(noteItems);
                items.addAll(noteItems);
            }
        }
        return items;
    }

    /**
     * Post process external <code>NoteItem</code>-s to make them ready to be displayed.
     * 
     * @param noteItems
     *            note items to be processed.
     */
    private void postProcess(Set<NoteItem> noteItems) {
        for (NoteItem item : noteItems) {
            HibNoteItem hibItem = (HibNoteItem) item;
            String eTag = hibItem.getEntityTag();
            if (eTag == null || eTag.trim().isEmpty()) {
                hibItem.setEntityTag(UUID.randomUUID().toString());
            }
        }
    }

    /* All below methods should not be called for external providers */

    @Override
    public void init() {
        throw new UnsupportedOperationException();
    }
<commit_msg>Handle null content from ContentSource#getContent method.<commit_after>package org.unitedinternet.cosmo.dao.external;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.unitedinternet.cosmo.dao.ContentDao;
import org.unitedinternet.cosmo.ext.ContentSource;
import org.unitedinternet.cosmo.model.BaseEventStamp;
import org.unitedinternet.cosmo.model.CalendarCollectionStamp;
import org.unitedinternet.cosmo.model.CollectionItem;
import org.unitedinternet.cosmo.model.ContentItem;
import org.unitedinternet.cosmo.model.HomeCollectionItem;
import org.unitedinternet.cosmo.model.Item;
import org.unitedinternet.cosmo.model.NoteItem;
import org.unitedinternet.cosmo.model.Ticket;
import org.unitedinternet.cosmo.model.User;
import org.unitedinternet.cosmo.model.filter.ItemFilter;
import org.unitedinternet.cosmo.model.hibernate.HibNoteItem;

import com.google.common.base.Strings;

/**
 * <code>ContentDao</code> that fetches calendar content from external providers.
 * 
 * @author <NAME>
 *
 */
public class ContentDaoExternal implements ContentDao {

    private static final Log LOG = LogFactory.getLog(ContentDaoExternal.class);

    private final ContentSource contentSource;

    private final ContentDao contentDaoInternal;

    public ContentDaoExternal(ContentSource contentSource, final ContentDao contentDaoInternal) {
        this.contentSource = contentSource;
        this.contentDaoInternal = contentDaoInternal;
    }

    @Override
    public Item findItemByPath(String path) {
        PathSegments extPath = new PathSegments(path);

        String homeCollectionUid = validHomeCollectionUidFrom(extPath);

        String collectionUid = validCollectionUidFrom(extPath);

        Item collectionItem = this.contentDaoInternal.findItemByPath(homeCollectionUid + "/" + collectionUid);

        if (collectionItem == null) {
            throw new IllegalArgumentException(
                    "Could not find collection for path: " + homeCollectionUid + "/" + collectionUid);
        }

        CalendarCollectionStamp stamp = (CalendarCollectionStamp) collectionItem
                .getStamp(CalendarCollectionStamp.class);

        if (stamp == null) {
            throw new IllegalStateException("Found calendar without stamp for path [" + path + "]");
        }

        String targetUri = stamp.getTargetUri();

        Set<NoteItem> itemsFromUri = this.contentSource.getContent(targetUri);
        if (itemsFromUri != null) {
            for (NoteItem item : itemsFromUri) {
                item.setOwner(collectionItem.getOwner());
            }

            String eventUid = extPath.getEventUid();

            if (Strings.isNullOrEmpty(eventUid)) {
                return new ExternalCollectionItem((CollectionItem) collectionItem, itemsFromUri);
            }
            for (NoteItem noteItem : itemsFromUri) {
                if (eventUid.equals(noteItem.getName())) {
                    return noteItem;
                }
            }
        }
        return null;
    }

    private static String validCollectionUidFrom(PathSegments extPath) {
        String collectionUid = extPath.getCollectionUid();

        if (collectionUid == null || collectionUid.trim().isEmpty()) {
            throw new IllegalArgumentException("Collection path cannot be null or empty.");
        }
        return collectionUid;
    }

    private static String validHomeCollectionUidFrom(PathSegments extPath) {
        String homeCollectionUid = extPath.getHomeCollectionUid();

        if (homeCollectionUid == null || homeCollectionUid.trim().isEmpty()) {
            throw new IllegalArgumentException("Home path path cannot be null or empty.");
        }
        return homeCollectionUid;
    }

    @Override
    public Item findItemByPath(String path, String parentUid) {
        LOG.info("EXTERNAL Delegating call to internal for parentUid: " + parentUid + " and path: " + path);
        return this.contentDaoInternal.findItemByPath(path, parentUid);
    }

    @Override
    public Set<Item> findItems(ItemFilter filter) {
        Set<Item> items = new HashSet<>();
        if (filter != null && filter.getParent() != null) {
            CollectionItem calendarItem = filter.getParent();
            CalendarCollectionStamp stamp = (CalendarCollectionStamp) calendarItem
                    .getStamp(CalendarCollectionStamp.class);
            if (stamp != null) {
                String targetUri = stamp.getTargetUri();
                Set<NoteItem> noteItems = this.contentSource.getContent(targetUri);
                if (noteItems != null) {
                    this.postProcess(noteItems);
                    items.addAll(noteItems);
                }
            }
        }
        return items;
    }

    /**
     * Post process external <code>NoteItem</code>-s to make them ready to be displayed.
     * 
     * @param noteItems
     *            note items to be processed.
     */
    private void postProcess(Set<NoteItem> noteItems) {
        for (NoteItem item : noteItems) {
            HibNoteItem hibItem = (HibNoteItem) item;
            String eTag = hibItem.getEntityTag();
            if (eTag == null || eTag.trim().isEmpty()) {
                hibItem.setEntityTag(UUID.randomUUID().toString());
            }
        }
    }

    /* All below methods should not be called for external providers */

    @Override
    public void init() {
        throw new UnsupportedOperationException();
    }

{'fraction_non_alphanumeric': 0.09201673031460265, 'fraction_numerical': 0.0, 'mean_word_length': 2.7642026009582477, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7154786', 'n_tokens_mistral': 2975, 'n_tokens_neox': 2939, 'n_words': 646}