Package com.ourlinc.helloworld.service.impl

Source Code of com.ourlinc.helloworld.service.impl.ActivityServiceImpl$ActivityPodiImpl

package com.ourlinc.helloworld.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.ourlinc.helloworld.DataSource;
import com.ourlinc.helloworld.model.Activity;
import com.ourlinc.helloworld.model.Comment;
import com.ourlinc.helloworld.model.User;
import com.ourlinc.helloworld.service.ActivityService;
import com.ourlinc.helloworld.service.UserService;
import com.ourlinc.omni.persistence.Persistence;
import com.ourlinc.omni.persistence.Persister;
import com.ourlinc.omni.persistence.PersisterStorage;
import com.ourlinc.omni.search.Searcher;
import com.ourlinc.swift.util.ResultPage;

/**
* 活动模块接口实现
*
* @author lipeiying
*
*/
public class ActivityServiceImpl implements ActivityService {
  final DataSource m_DataSource; // 数据源
  final ActivityPodiImpl m_Podi;
  UserService m_userService;

  public ActivityServiceImpl(DataSource ds) {
    m_DataSource = ds;
    m_Podi = new ActivityPodiImpl();
  }

  public void setUserService(UserService userService) {
    m_userService = userService;
  }

  // 内部类实现ActivityPodi
  class ActivityPodiImpl implements ActivityPodi {
    final PersisterStorage<Activity> psAct;
    final PersisterStorage<Comment> psCom;

    ActivityPodiImpl() {
      psAct = m_DataSource.newPersister(Activity.class, this);
      psCom = m_DataSource.newPersister(Comment.class, this);
    }

    public Searcher getSearcher() {
      return null;
    }

    @SuppressWarnings("unchecked")
    public <E extends Persistence> Persister<E> getPersister(
        Class<E> classOf) {
      if (Activity.class == classOf) {
        return (Persister<E>) psAct;
      } else if (Comment.class == classOf) {
        return (Persister<E>) psCom;
      }
      return m_DataSource.getPersister(classOf);
    }

    public User getUserById(String id) {
      return m_userService.getUser(id);
    }

    public Comment postComment(Activity activity, User user, String content) {
      return new Comment(m_Podi, activity, user, content);
    }

  }

  public Activity addActivity(String title, Date time, String place,
      String content, Date startDate, Date overDate, String note) {
    return new Activity(m_Podi, title, time, place, content, startDate,
        overDate, note);

  }

  public Activity getActivity(String actId) {
    return m_Podi.psAct.get(actId);
  }

  public ResultPage<Activity> listAfter() {
    List<Activity> result = new ArrayList<Activity>();
    ResultPage<Activity> rp = m_Podi.psAct.startsWith(null);
    rp.setPageSize(128);
    while (rp.gotoPage(rp.getPage() + 1)) {
      while (rp.hasNext()) {
        Activity act = rp.next();
        if (new Date().after(act.getTime())) {
          result.add(act);
        }
      }
    }
    Collections.sort(result);
    return ResultPage.OnList.wrap(result);
  }

  public ResultPage<Activity> listBefore() {
    List<Activity> result = new ArrayList<Activity>();
    ResultPage<Activity> rp = m_Podi.psAct.startsWith(null);
    rp.setPageSize(128);
    Date now = new Date();
    while (rp.gotoPage(rp.getPage() + 1)) {
      while (rp.hasNext()) {
        Activity act = rp.next();
        if (now.before(act.getTime())
            && Activity.STATUS_AVAILIABLE == act.getStatus()) {
          result.add(act);
        }
      }
    }
    Collections.sort(result);
    return ResultPage.OnList.wrap(result);
  }

  public ResultPage<Activity> listCancel() {
    List<Activity> result = new ArrayList<Activity>();
    ResultPage<Activity> rp = m_Podi.psAct.startsWith(null);
    rp.setPageSize(128);
    while (rp.gotoPage(rp.getPage() + 1)) {
      while (rp.hasNext()) {
        Activity act = rp.next();
        if (Activity.STATUS_CANCEL == act.getStatus()
            && new Date().before(act.getTime())) {
          result.add(act);
        }
      }
    }
    Collections.sort(result);
    return ResultPage.OnList.wrap(result);
  }

  public ResultPage<Comment> listComment(String activityId) {
    if (null == activityId) {
      throw new NullPointerException("listComment方法的参数不能空");
    }
    List<Comment> result = new ArrayList<Comment>();
    ResultPage<Comment> rp = m_Podi.psCom.startsWith(null);
    rp.setPageSize(128);
    while (rp.gotoPage(rp.getPage() + 1)) {
      while (rp.hasNext()) {
        Comment com = rp.next();
        if (activityId.equals(com.getActivityId())
            && Comment.AVAILABLE == com.getStatus()) {
          result.add(com);
        }
      }
    }
    return ResultPage.OnList.wrap(result);
  }

  public Comment getComment(String comId) {
    return m_Podi.psCom.get(comId);
  }

  public ResultPage<Activity> searchActivity(String title, int year, int month) {
    List<Activity> result = new ArrayList<Activity>();
    ResultPage<Activity> rp = m_Podi.psAct.startsWith(null);
    rp.setPageSize(128);
    while (rp.gotoPage(rp.getPage() + 1)) {
      while (rp.hasNext()) {
        Activity act = rp.next();
        if (act.getTitle().contains(title)
            && isContainsDate(act.getTime(), year, month)) {
          result.add(act);
        }
      }
    }
    Collections.sort(result);
    return ResultPage.OnList.wrap(result);
  }

  /**
   * 判断date参数的年份和月份是否等于year,month,year和month等于0总是返回true,
   * year等于0而month不等于0默认取今年年份,如果year不等于0而month等于0表示忽略月份
   *
   * @param date
   * @param year
   * @param month
   * @return
   */
  private boolean isContainsDate(Date date, int year, int month) {
    if (null == date) {
      return false;
    }
    if (0 == year && 0 == month) {
      return true;
    } else if (0 == year && 0 != month) {
      Calendar c = Calendar.getInstance();
      int thisYear = c.get(Calendar.YEAR);
      Calendar cd = Calendar.getInstance();
      cd.setTime(date);
      int cdYear = cd.get(Calendar.YEAR);
      int cdMonth = cd.get(Calendar.MONTH) + 1;
      if (cdYear == thisYear && cdMonth == month) {
        return true;
      } else {
        return false;
      }

    } else if (0 != year && 0 == month) {// 忽略月份
      Calendar c = Calendar.getInstance();
      c.setTime(date);
      if (c.get(Calendar.YEAR) == year) {
        return true;
      } else {
        return false;
      }
    } else {
      Calendar c = Calendar.getInstance();
      c.setTime(date);
      if (c.get(Calendar.YEAR) == year
          && (c.get(Calendar.MONTH) + 1) == month) {
        return true;
      } else {
        return false;
      }

    }
  }

  public ResultPage<Activity> listAll() {
    List<Activity> result = new ArrayList<Activity>();
    ResultPage<Activity> rp = m_Podi.psAct.startsWith(null);
    rp.setPageSize(128);
    while (rp.gotoPage(rp.getPage() + 1)) {
      while (rp.hasNext()) {
        Activity act = rp.next();
        result.add(act);
      }
    }
    return ResultPage.OnList.wrap(result);
  }

  public void deleteActivity(String actId) {
    m_Podi.psAct.remove(actId);

  }

}
TOP

Related Classes of com.ourlinc.helloworld.service.impl.ActivityServiceImpl$ActivityPodiImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.