001/* 002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Alexandre Russel 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.platform.mail.action; 023 024import java.util.HashMap; 025import java.util.Map; 026 027import org.nuxeo.common.xmap.annotation.XNode; 028import org.nuxeo.common.xmap.annotation.XNodeList; 029import org.nuxeo.common.xmap.annotation.XObject; 030 031/** 032 * @author Alexandre Russel 033 * @author Laurent Doguin 034 */ 035@XObject("pipe") 036public class MessageActionPipeDescriptor { 037 038 private static final String START_ACTION = "StartAction"; 039 040 @XNode("@name") 041 private String name; 042 043 @XNode("@override") 044 private Boolean override; 045 046 private final Map<String, MessageActionDescriptor> actionDescriptorsRegistry = new HashMap<String, MessageActionDescriptor>(); 047 048 private MessageActionPipe pipe = new MessageActionPipe(); 049 050 @XNodeList(value = "action", type = MessageActionDescriptor[].class, componentType = MessageActionDescriptor.class) 051 MessageActionDescriptor[] actions; 052 053 public String getName() { 054 return name; 055 } 056 057 public boolean getOverride() { 058 if (override == null) { 059 return false; 060 } 061 return override; 062 } 063 064 public Map<String, MessageActionDescriptor> getActions() { 065 for (MessageActionDescriptor action : actions) { 066 actionDescriptorsRegistry.put(action.getId(), action); 067 } 068 return actionDescriptorsRegistry; 069 } 070 071 /** 072 * Merge this MessageActionPipeDescriptor with the given one. 073 */ 074 public void merge(MessageActionPipeDescriptor descriptor) { 075 for (String actionName : descriptor.getActions().keySet()) { 076 actionDescriptorsRegistry.put(actionName, descriptor.getActions().get(actionName)); 077 } 078 pipe = new MessageActionPipe(); 079 } 080 081 /** 082 * @return initialized action pipe 083 */ 084 public MessageActionPipe getPipe() { 085 if (pipe.isEmpty()) { 086 fillMissingActionDestination(); 087 MessageActionDescriptor initialAction = getActions().get(START_ACTION); 088 addAction(initialAction); 089 } 090 return pipe; 091 } 092 093 private void addAction(MessageActionDescriptor msgActionDescriptor) { 094 pipe.add(msgActionDescriptor.getAction()); 095 String next = msgActionDescriptor.getTo(); 096 MessageActionDescriptor nextAction = actionDescriptorsRegistry.get(next); 097 if (nextAction == null) { 098 return; 099 } else { 100 addAction(nextAction); 101 } 102 } 103 104 /** 105 * Fill empty destination using registering order to preserve backward compatibility. 106 */ 107 private void fillMissingActionDestination() { 108 for (int i = 0; i < actions.length - 1; i++) { 109 String destination = actions[i].getTo(); 110 if (destination == null || "".equals(destination)) { 111 String newDestination = actions[i + 1].getId(); 112 if (newDestination != null) { 113 actions[i].setTo(newDestination); 114 } 115 } 116 } 117 } 118}