/*
 * Copyright (C) 2003-2007 eXo Platform SAS.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see<http://www.gnu.org/licenses/>.
 */
package org.exoplatform.ecm.webui.component.admin.unlock;

import java.util.List;

import org.exoplatform.commons.utils.LazyPageList;
import org.exoplatform.commons.utils.ListAccess;
import org.exoplatform.commons.utils.ListAccessImpl;
import org.exoplatform.ecm.webui.core.UIPagingGridDecorator;
import org.exoplatform.services.cms.lock.LockService;
import org.exoplatform.webui.config.annotation.ComponentConfig;
import org.exoplatform.webui.config.annotation.EventConfig;
import org.exoplatform.webui.event.Event;
import org.exoplatform.webui.event.EventListener;

/**
 * Created by The eXo Platform SARL
 * Author : Dang Van Minh
 *          minh.dang@exoplatform.com
 * Dec 29, 2006
 * 11:30:17 AM
 */
@ComponentConfig(
    template = "app:/groovy/webui/component/admin/unlock/UILockList.gtmpl",
    events = {
        @EventConfig(listeners = UILockList.DeleteLockActionListener.class)
    }
)
public class UILockList extends UIPagingGridDecorator {
  final static public String[] ACTIONS = {};

  final static public String   ST_EDIT = "EditUnLockForm";

  public UILockList() throws Exception {
    getUIPageIterator().setId("LockListIterator");
  }

  public String[] getActions() {
    return ACTIONS;
  }

  public void refresh(int currentPage) throws Exception {
    ListAccess<String> groupsAndUsersForLockList = new ListAccessImpl<String>(String.class,
                                                                              getAllGroupsOrUsersForLock());
    LazyPageList<String> pageList = new LazyPageList<String>(groupsAndUsersForLockList,
                                                             getUIPageIterator().getItemsPerPage());
    getUIPageIterator().setPageList(pageList);
    if (currentPage > getUIPageIterator().getAvailablePage())
      getUIPageIterator().setCurrentPage(getUIPageIterator().getAvailablePage());
    else
      getUIPageIterator().setCurrentPage(currentPage);
  }

  public List getGroupsOrUsersForLock() throws Exception {
    return getUIPageIterator().getCurrentPageData();
  }

  public List<String> getAllGroupsOrUsersForLock() throws Exception {
    LockService lockService = getApplicationComponent(LockService.class);

    return lockService.getAllGroupsOrUsersForLock();
  }

  static public class DeleteLockActionListener extends EventListener<UILockList> {
    public void execute(Event<UILockList> event) throws Exception {
      UIUnLockManager uiUnLockManager = event.getSource().getParent();
      String settingLock = event.getRequestContext().getRequestParameter(OBJECTID);
      LockService lockService = uiUnLockManager.getApplicationComponent(LockService.class);
      lockService.removeGroupsOrUsersForLock(settingLock);
      UILockList uiLockList = uiUnLockManager.getChild(UILockList.class);
      uiLockList.refresh(uiLockList.getUIPageIterator().getCurrentPage());
      event.getRequestContext().addUIComponentToUpdateByAjax(uiUnLockManager);
    }
  }
}
