1   /*
2    * Copyright 2003 - 2013 The eFaps Team
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Revision:        $Rev$
17   * Last Changed:    $Date$
18   * Last Changed By: $Author$
19   */
20  
21  package org.efaps.update.schema.ui;
22  
23  import java.net.URL;
24  import java.util.HashSet;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import org.efaps.admin.event.EventType;
30  import org.efaps.update.AbstractUpdate;
31  import org.efaps.update.LinkInstance;
32  import org.efaps.update.event.Event;
33  import org.efaps.util.EFapsException;
34  
35  /**
36   * This Class is responsible for the Update of "Command" in the Database.
37   *
38   * @author The eFaps Team
39   * @version $Id$
40   */
41  public class CommandUpdate
42      extends AbstractUpdate
43  {
44      /**
45       * Set of all links used by commands.
46       */
47      protected static final Set<Link> ALLLINKS = new HashSet<Link>();
48  
49      /** Link from UI object to role. */
50      private static final Link LINK2ACCESSROLE = new Link("Admin_UI_Access", "UILink",
51                      "Admin_User_RoleAbstract", "UserLink").setIncludeChildTypes(true);
52  
53      /** Link from UI object to person. */
54      private static final Link LINK2ACCESSPERSON = new Link("Admin_UI_Access", "UILink", "Admin_User_Person",
55                                                             "UserLink");
56  
57      /** Link from UI object to group. */
58      private static final Link LINK2ACCESSGROUP = new Link("Admin_UI_Access", "UILink", "Admin_User_Group", "UserLink");
59  
60      /** Link from UI object to company. */
61      private static final Link LINK2ACCESSCOMPANY = new Link("Admin_UI_Access", "UILink", "Admin_User_Company",
62                                                              "UserLink");
63  
64      /** Link from command to icon. */
65      private static final Link LINK2ICON = new UniqueLink("Admin_UI_LinkIcon", "From", "Admin_UI_Image", "To");
66  
67      /** Link from command to command as target. */
68      private static final Link LINK2TARGETCMD = new UniqueLink("Admin_UI_LinkTargetCommand", "From", "Admin_UI_Command",
69                      "To");
70  
71      /** Link from command to table as target. */
72      private static final Link LINK2TARGETTABLE = new UniqueLink("Admin_UI_LinkTargetTable", "From", "Admin_UI_Table",
73                      "To");
74  
75      /** Link from command to form as target. */
76      private static final Link LINK2TARGETFORM = new UniqueLink("Admin_UI_LinkTargetForm", "From", "Admin_UI_Form",
77                      "To");
78  
79      /** Link from command to menu as target. */
80      private static final Link LINK2TARGETMENU = new UniqueLink("Admin_UI_LinkTargetMenu", "From", "Admin_UI_Menu",
81                      "To");
82  
83      /** Link from command to search as target. */
84      private static final Link LINK2TARGETSEARCH = new UniqueLink("Admin_UI_LinkTargetSearch", "From", "Admin_UI_Search",
85                                                             "To");
86  
87      /** Link from command to wiki. */
88      private static final Link LINK2TARGETHELP = new UniqueLink("Admin_UI_LinkTargetHelp", "FromMenuLink",
89                                                           "Admin_Program_Wiki", "ToWikiLink");
90  
91      static {
92          CommandUpdate.ALLLINKS.add(CommandUpdate.LINK2ACCESSROLE);
93          CommandUpdate.ALLLINKS.add(CommandUpdate.LINK2ACCESSPERSON);
94          CommandUpdate.ALLLINKS.add(CommandUpdate.LINK2ACCESSGROUP);
95          CommandUpdate.ALLLINKS.add(CommandUpdate.LINK2ACCESSCOMPANY);
96          CommandUpdate.ALLLINKS.add(CommandUpdate.LINK2ICON);
97          CommandUpdate.ALLLINKS.add(CommandUpdate.LINK2TARGETCMD);
98          CommandUpdate.ALLLINKS.add(CommandUpdate.LINK2TARGETTABLE);
99          CommandUpdate.ALLLINKS.add(CommandUpdate.LINK2TARGETFORM);
100         CommandUpdate.ALLLINKS.add(CommandUpdate.LINK2TARGETMENU);
101         CommandUpdate.ALLLINKS.add(CommandUpdate.LINK2TARGETSEARCH);
102         CommandUpdate.ALLLINKS.add(CommandUpdate.LINK2TARGETHELP);
103     }
104 
105     /**
106      * Default contractor used by the XML parser for initialize a command.
107      *
108      * @param _url URL of the file
109      */
110     public CommandUpdate(final URL _url)
111     {
112         super(_url, "Admin_UI_Command", CommandUpdate.ALLLINKS);
113     }
114 
115     /**
116      * @param _url url
117      * @param _typeName name of the type
118      * @param _allLinks all links
119      */
120     protected CommandUpdate(final URL _url,
121                             final String _typeName,
122                             final Set<Link> _allLinks)
123     {
124         super(_url, _typeName, _allLinks);
125     }
126 
127     /**
128      * Creates new instance of class {@link CommandDefinition}.
129      *
130      * @return new definition instance
131      * @see CommandDefinition
132      */
133     @Override
134     protected AbstractDefinition newDefinition()
135     {
136         return new CommandDefinition();
137     }
138 
139     /**
140      * Definition for a command.
141      *
142      */
143     protected class CommandDefinition
144         extends AbstractDefinition
145     {
146 
147         /**
148          * Read the xml.
149          *
150          * @param _tags List of tags
151          * @param _attributes map of attributes
152          * @param _text text
153          * @throws EFapsException on error
154          */
155         @Override
156         protected void readXML(final List<String> _tags,
157                                final Map<String, String> _attributes,
158                                final String _text)
159             throws EFapsException
160         {
161             final String value = _tags.get(0);
162             if ("access".equals(value)) {
163                 if (_tags.size() > 1) {
164                     final String subValue = _tags.get(1);
165                     if ("role".equals(subValue)) {
166                         // Assigns a role for accessing this command.
167                         addLink(CommandUpdate.LINK2ACCESSROLE, new LinkInstance(_text));
168                     } else if ("person".equals(subValue)) {
169                         // Assigns a person for accessing this command.
170                         addLink(CommandUpdate.LINK2ACCESSPERSON, new LinkInstance(_text));
171                     } else if ("group".equals(subValue)) {
172                         // Assigns a group for accessing this command.
173                         addLink(CommandUpdate.LINK2ACCESSGROUP, new LinkInstance(_text));
174                     } else if ("company".equals(subValue)) {
175                         // Assigns a company for accessing this command.
176                         addLink(CommandUpdate.LINK2ACCESSCOMPANY, new LinkInstance(_text));
177                     } else {
178                         super.readXML(_tags, _attributes, _text);
179                     }
180                 }
181             } else if ("icon".equals(value)) {
182                 // Assigns an image
183                 addLink(CommandUpdate.LINK2ICON, new LinkInstance(_text));
184             } else if ("target".equals(value)) {
185                 if (_tags.size() == 2) {
186                     final String subValue = _tags.get(1);
187                     final String name = (_attributes.get("name") == null
188                                     ? getValue("Name") + "." + subValue : _attributes.get("name"))
189                                                     + (_attributes.get("index") == null
190                                                     ? "" : ("." + _attributes.get("index")));
191                     if ("evaluate".equals(subValue)) {
192                         addEvent(new Event(name, EventType.UI_TABLE_EVALUATE,
193                                         _attributes.get("program"), _attributes.get("method"),
194                                         _attributes.get("index")));
195                     } else if ("execute".equals(subValue)) {
196                         addEvent(new Event(name, EventType.UI_COMMAND_EXECUTE,
197                                         _attributes.get("program"), _attributes.get("method"),
198                                         _attributes.get("index")));
199                     } else if ("instance".equals(subValue)) {
200                         addEvent(new Event(name, EventType.UI_INSTANCEMANAGER,
201                                         _attributes.get("program"), _attributes.get("method"),
202                                         _attributes.get("index")));
203                     } else if ("command".equals(subValue)) {
204                         // assigns a command as target for this command definition.
205                         addLink(CommandUpdate.LINK2TARGETCMD, new LinkInstance(_text));
206                     } else if ("form".equals(subValue)) {
207                         // assigns a form as target for this command definition.
208                         addLink(CommandUpdate.LINK2TARGETFORM, new LinkInstance(_text));
209                     } else if ("menu".equals(subValue)) {
210                         // assigns a menu as target for this command definition.
211                         addLink(CommandUpdate.LINK2TARGETMENU, new LinkInstance(_text));
212                     } else if ("search".equals(subValue)) {
213                         // assigns a search menu as target for this command definition.
214                         addLink(CommandUpdate.LINK2TARGETSEARCH, new LinkInstance(_text));
215                     } else if ("table".equals(subValue)) {
216                         // assigns a table as target for this command definition.
217                         addLink(CommandUpdate.LINK2TARGETTABLE, new LinkInstance(_text));
218                     } else if ("trigger".equals(subValue)) {
219                         addEvent(new Event(_attributes.get("name"), EventType.valueOf(_attributes.get("event")),
220                                         _attributes.get("program"), _attributes.get("method"),
221                                         _attributes.get("index")));
222                     } else if ("validate".equals(subValue)) {
223                         addEvent(new Event(name, EventType.UI_VALIDATE,
224                                         _attributes.get("program"), _attributes.get("method"),
225                                         _attributes.get("index")));
226                     } else if ("help".equals(subValue)) {
227                         // assigns a wiki as target for this command definition.
228                         addLink(CommandUpdate.LINK2TARGETHELP, new LinkInstance(_text));
229                     } else {
230                         super.readXML(_tags, _attributes, _text);
231                     }
232                 } else if (_tags.size() == 3) {
233                     final String subValue = _tags.get(1);
234                     if (("evaluate".equals(subValue) || "execute".equals(subValue) || "trigger".equals(subValue)
235                                     || "validate".equals(subValue)) && "property".equals(_tags.get(2))) {
236                         getEvents().get(getEvents().size() - 1).addProperty(_attributes.get("name"), _text);
237                     } else {
238                         super.readXML(_tags, _attributes, _text);
239                     }
240                 } else if (_tags.size() > 2) {
241                     // if size == 1, the target tag could be ignored
242                     super.readXML(_tags, _attributes, _text);
243                 }
244             } else {
245                 super.readXML(_tags, _attributes, _text);
246             }
247         }
248     }
249 }