/** * C-Nery - A home automation web group for C-Bus. * Copyright (C) 2008,2009,2012 Dave Oxley <dave@daveoxley.co.uk>. * * 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 Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ package com.daveoxley.cnery.services; import com.daveoxley.cbus.Application; import com.daveoxley.cbus.Group; import com.daveoxley.seam.SelectionService; import com.daveoxley.seam.exceptions.EntityNotFoundException; import java.io.Serializable; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.jboss.seam.ScopeType; import org.jboss.seam.annotations.In; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.Observer; import org.jboss.seam.annotations.Out; import org.jboss.seam.annotations.Scope; import org.jboss.seam.core.Events; /** * * @author Dave Oxley <dave@daveoxley.co.uk> */ @Name("groupSelectionService") @Scope(ScopeType.PAGE) public class GroupSelectionService extends SelectionService<Integer> implements Serializable { private final static Log log = LogFactory.getLog(GroupSelectionService.class); @In private Events events; @In(required=false) private Application selectedApplication; @Out(value="selectedGroupAddresses",required=false,scope=ScopeType.PAGE) @Override public List<Integer> getSelectedIds() { return super.getSelectedIds(); } @Override public void setSelectedIds(List<Integer> selectedIds) throws EntityNotFoundException { super.setSelectedIds(selectedIds); } @Override protected Integer cast(Object o) { return (Integer)o; } @Out(required=false) public Group[] getSelectedGroups() throws Exception { List<Integer> selectedGroupAddresses = getSelectedIds(); if (selectedGroupAddresses == null || selectedGroupAddresses.isEmpty()) return null; Group[] selectedGroups = new Group[selectedGroupAddresses.size()]; for (int i = 0; i < selectedGroupAddresses.size(); i++) { try { selectedGroups[i] = selectedApplication.getGroup(selectedGroupAddresses.get(i)); } catch (Exception ex) { throw new EntityNotFoundException(selectedGroupAddresses.get(i)); } } return selectedGroups; } @Observer(create=false,value="clearGroupSelection") public void clearGroupSelection() { events.raiseEvent("groupsUpdated", (Object)null); } @Observer(create=false,value="groupsUpdated") public void updateGroupSelection(List<Integer> selectionKeys) throws EntityNotFoundException { setSelectedIds(selectionKeys); } }