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.access;
22  
23  
24  import java.net.URL;
25  import java.util.HashSet;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Set;
29  
30  import org.efaps.admin.datamodel.Type;
31  import org.efaps.db.Instance;
32  import org.efaps.update.AbstractUpdate;
33  import org.efaps.update.LinkInstance;
34  import org.efaps.util.EFapsException;
35  
36  /**
37   * @author The eFaps Team
38   * @version $Id$
39   */
40  public class AccessSetUpdate
41      extends AbstractUpdate
42  {
43      /** Link to access types. */
44      private static final Link LINK2ACCESSTYPE = new Link("Admin_Access_AccessSet2Type", "AccessSetLink",
45                                                           "Admin_Access_AccessType", "AccessTypeLink");
46  
47      /** Link to data model types. */
48      private static final Link LINK2DATAMODELTYPE = new Link("Admin_Access_AccessSet2DataModelType", "AccessSetLink",
49                                                              "Admin_DataModel_Type", "DataModelTypeLink");
50  
51      /** Link to Status. */
52      private static final Link LINK2STATUS = new Link("Admin_Access_AccessSet2Status", "AccessSetLink",
53                                                       "Admin_DataModel_StatusAbstract",
54                                                       "SatusLink", "Type", "Key").setIncludeChildTypes(true);
55  
56      /** Link to persons. */
57      private static final Link LINK2PERSON = new Link("Admin_Access_AccessSet2UserAbstract", "AccessSetLink",
58                                                       "Admin_User_Person", "UserAbstractLink");
59  
60      /** Link to roles. */
61      private static final Link LINK2ROLE = new Link("Admin_Access_AccessSet2UserAbstract", "AccessSetLink",
62                                                     "Admin_User_RoleAbstract", "UserAbstractLink");
63  
64      /** Link to groups. */
65      private static final Link LINK2GROUP = new Link("Admin_Access_AccessSet2UserAbstract", "AccessSetLink",
66                                                      "Admin_User_Group", "UserAbstractLink");
67  
68      /**
69       * Map of all links.
70       */
71      private static final Set<Link> ALLLINKS = new HashSet<Link>();
72      static {
73          AccessSetUpdate.ALLLINKS.add(AccessSetUpdate.LINK2ACCESSTYPE);
74          AccessSetUpdate.ALLLINKS.add(AccessSetUpdate.LINK2DATAMODELTYPE);
75          AccessSetUpdate.ALLLINKS.add(AccessSetUpdate.LINK2STATUS);
76          AccessSetUpdate.ALLLINKS.add(AccessSetUpdate.LINK2PERSON);
77          AccessSetUpdate.ALLLINKS.add(AccessSetUpdate.LINK2ROLE);
78          AccessSetUpdate.ALLLINKS.add(AccessSetUpdate.LINK2GROUP);
79      }
80  
81      /**
82       *
83       * @param _url URL of the file
84       */
85      public AccessSetUpdate(final URL _url)
86      {
87          super(_url, "Admin_Access_AccessSet", AccessSetUpdate.ALLLINKS);
88      }
89  
90      /**
91       * Creates new instance of class {@link AccessSetUpdate.Definition}.
92       *
93       * @return new definition instance
94       * @see AccessSetUpdate.Definition
95       */
96      @Override
97      protected AbstractDefinition newDefinition()
98      {
99          return new Definition();
100     }
101 
102     /**
103      * Definition.
104      */
105     private class Definition extends AbstractDefinition
106     {
107         /**
108          * Name of the current status group.
109          */
110         private String currentGroupName;
111 
112         /**
113          * {@inheritDoc}
114          * @throws EFapsException
115          */
116         @Override
117         protected void readXML(final List<String> _tags,
118                                final Map<String, String> _attributes,
119                                final String _text)
120             throws EFapsException
121         {
122             final String value = _tags.get(0);
123             if ("access-type".equals(value)) {
124                 addLink(AccessSetUpdate.LINK2ACCESSTYPE, new LinkInstance(_text));
125             } else if ("type".equals(value)) {
126                 addLink(AccessSetUpdate.LINK2DATAMODELTYPE, new LinkInstance(_text));
127             } else if ("group".equals(value)) {
128                 addLink(AccessSetUpdate.LINK2GROUP, new LinkInstance(_text));
129             } else if ("person".equals(value)) {
130                 addLink(AccessSetUpdate.LINK2PERSON, new LinkInstance(_text));
131             } else if ("role".equals(value)) {
132                 addLink(AccessSetUpdate.LINK2ROLE, new LinkInstance(_text));
133             } else if ("status".equals(value)) {
134                 if (_tags.size() == 1) {
135                     this.currentGroupName = _attributes.get("group");
136                 } else if ((_tags.size() == 2) && "key".equals(_tags.get(1))) {
137                     final LinkInstance linkinstance = new LinkInstance();
138                     linkinstance.getKeyAttr2Value().put("Key", _text);
139                     linkinstance.getKeyAttr2Value().put("Type", this.currentGroupName);
140                     addLink(AccessSetUpdate.LINK2STATUS, linkinstance);
141                 }
142             } else {
143                 super.readXML(_tags, _attributes, _text);
144             }
145         }
146 
147         /**
148          * {@inheritDoc}
149          */
150         @Override
151         protected void setLinksInDB(final Instance _instance,
152                                     final Link _linktype,
153                                     final Set<LinkInstance> _links)
154             throws EFapsException
155         {
156             if (_links != null) {
157                 for (final LinkInstance linkInst : _links) {
158                     if (linkInst.getKeyAttr2Value().containsKey("Type")) {
159                         final String typeName = linkInst.getKeyAttr2Value().get("Type");
160                         linkInst.getKeyAttr2Value().put("Type", ((Long) Type.get(typeName).getId()).toString());
161                     }
162                 }
163             }
164             super.setLinksInDB(_instance, _linktype, _links);
165         }
166     }
167 }