public void deleteSubscription(
     Long userId, Long classNameId, Long classPK, Integer type, String extraData)
     throws SystemException {
   List<ActivitySubscription> subscriptions =
       activitySubscriptionPersistence.findByClassNameIdClassPKTypeExtraDataReceiverId(
           classNameId, classPK, type, extraData, userId);
   for (ActivitySubscription subscription : subscriptions) {
     subscription.delete();
   }
 }
  public void addSubscription(
      Long classNameId, Long classPK, Integer type, String extraData, Long userId)
      throws PortalException, SystemException {

    if (activitySubscriptionPersistence
            .findByClassNameIdClassPKTypeExtraDataReceiverId(
                classNameId, classPK, type, extraData, userId)
            .size()
        > 0) {
      // subscription exists, do nothing
      return;
    }

    Long pk = CounterLocalServiceUtil.increment(ActivitySubscription.class.getName());
    ActivitySubscription subscription =
        ActivitySubscriptionLocalServiceUtil.createActivitySubscription(pk);

    subscription.setClassNameId(classNameId);
    subscription.setClassPK(classPK);
    subscription.setType(type);
    subscription.setExtraData(extraData);
    subscription.setReceiverId(userId);

    subscription.setModifiedDate(new Date());
    subscription.setCreateDate(new Date());

    subscription.store();
  }
  public List<SocialActivity> getActivities(Long userId, int start, int count)
      throws SystemException {
    List<ActivitySubscription> subscriptions = null;
    // for now no activity selection is made, TODO
    subscriptions = activitySubscriptionPersistence.findByreceiverId(userId);

    if (subscriptions.size() == 0) {
      return new ArrayList<SocialActivity>();
    }

    DynamicQuery query = DynamicQueryFactoryUtil.forClass(SocialActivity.class);
    Criterion crit = null;

    for (ActivitySubscription sub : subscriptions) {
      Map<String, Number> criterion = new HashMap<String, Number>();
      criterion.put("classNameId", sub.getClassNameId());
      criterion.put("classPK", sub.getClassPK());

      if (sub.getType() != null) {
        criterion.put("type", sub.getType());
      }
      Criterion subCriterion = RestrictionsFactoryUtil.allEq(criterion);

      if (sub.getExtraData() != null && sub.getExtraData().length() > 0) {
        subCriterion =
            RestrictionsFactoryUtil.and(
                subCriterion, RestrictionsFactoryUtil.ilike("extraData", sub.getExtraData() + "%"));
      }

      if (crit == null) {
        crit = subCriterion;
      } else {
        crit = RestrictionsFactoryUtil.or(crit, subCriterion);
      }
    }
    query.add(crit).addOrder(OrderFactoryUtil.desc("createDate"));

    List<SocialActivity> activities = new ArrayList<SocialActivity>();
    List<Object> queryResults =
        SocialActivityLocalServiceUtil.dynamicQuery(query, start, start + count - 1);

    for (Object activity : queryResults) {
      activities.add((SocialActivity) activity);
    }

    return activities;
  }