001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/common/api-impl/src/java/org/theospi/portfolio/shared/model/impl/ViewerCustomEditor.java $
003: * $Id:ViewerCustomEditor.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.theospi.portfolio.shared.model.impl;
021:
022: import java.beans.PropertyEditorSupport;
023: import java.util.Collection;
024: import java.util.HashSet;
025: import java.util.Iterator;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.sakaiproject.metaobj.shared.mgt.AgentManager;
030: import org.sakaiproject.metaobj.shared.mgt.IdManager;
031: import org.sakaiproject.metaobj.shared.model.Agent;
032: import org.sakaiproject.metaobj.shared.model.impl.AgentImpl;
033: import org.sakaiproject.metaobj.utils.mvc.intf.TypedPropertyEditor;
034:
035: /**
036: * Created by IntelliJ IDEA.
037: * User: John Ellis
038: * Date: May 19, 2004
039: * Time: 2:23:28 PM
040: * To change this template use File | Settings | File Templates.
041: */
042: public class ViewerCustomEditor extends PropertyEditorSupport implements
043: TypedPropertyEditor {
044: protected final Log logger = LogFactory.getLog(this .getClass());
045: private IdManager idManager = null;
046: private AgentManager agentManager;
047:
048: public void setAsText(String text) throws IllegalArgumentException {
049: if (text == null || text.length() == 0) {
050: setValue(new HashSet());
051: } else {
052: String[] items = text.split(",");
053: Collection viewers = new HashSet();
054:
055: for (int i = 0; i < items.length; i++) {
056: Agent agent = getAgentManager().getAgent(items[i]);
057: if (agent == null) {
058: agent = createGuest(items[i]);
059: }
060: viewers.add(agent);
061: }
062: setValue(viewers);
063: }
064: }
065:
066: protected Agent createGuest(String item) {
067: AgentImpl viewer = new AgentImpl();
068: viewer.setDisplayName(item);
069: viewer.setRole(Agent.ROLE_GUEST);
070: viewer.setId(getIdManager().getId(viewer.getDisplayName()));
071: return viewer;
072: }
073:
074: public String getAsText() {
075: StringBuffer buffer = new StringBuffer();
076: for (Iterator i = ((Collection) getValue()).iterator(); i
077: .hasNext();) {
078: Agent agent = (Agent) i.next();
079: buffer.append(agent.getId().getValue());
080: }
081: return buffer.toString();
082: }
083:
084: public AgentManager getAgentManager() {
085: return agentManager;
086: }
087:
088: public void setAgentManager(AgentManager agentManager) {
089: this .agentManager = agentManager;
090: }
091:
092: public Class getType() {
093: return Collection.class;
094: }
095:
096: public IdManager getIdManager() {
097: return idManager;
098: }
099:
100: public void setIdManager(IdManager idManager) {
101: this.idManager = idManager;
102: }
103:
104: }
|