1   /*
2    * Copyright 2003 - 2015 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.user;
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.db.Instance;
30  import org.efaps.update.AbstractUpdate;
31  import org.efaps.update.LinkInstance;
32  import org.efaps.util.EFapsException;
33  
34  /**
35   * @author The eFaps Team
36   * @version $Id$
37   * TODO:  description
38   */
39  public class RoleUpdate
40      extends AbstractUpdate
41  {
42      /**
43       * Set of all links used by commands.
44       */
45      private static final Set<Link> ALLLINKS = new HashSet<>();
46  
47      /** Link from UI object to role. */
48      private static final Link LINK2ACCESSCMD = new Link("Admin_UI_Access", "UserLink",
49                      "Admin_UI_Command", "UILink").setIncludeChildTypes(true);
50  
51      /** Link from AccessSet to roles. */
52      private static final Link LINK2ACCESSSET = new Link("Admin_Access_AccessSet2UserAbstract", "UserAbstractLink",
53                                                     "Admin_Access_AccessSet", "AccessSetLink");
54  
55      static {
56          RoleUpdate.ALLLINKS.add(RoleUpdate.LINK2ACCESSCMD);
57          RoleUpdate.ALLLINKS.add(RoleUpdate.LINK2ACCESSSET);
58      }
59  
60      /**
61       * Global type or local.
62       */
63      private boolean global = true;
64  
65      /**
66       *
67       * @param _url        URL of the file
68       */
69      public RoleUpdate(final URL _url)
70      {
71          super(_url, "temp", RoleUpdate.ALLLINKS);
72      }
73  
74      @Override
75      public String getDataModelTypeName()
76      {
77          return this.global ? "Admin_User_RoleGlobal" : "Admin_User_RoleLocal";
78      }
79  
80      /**
81       * Creates new instance of class {@link RoleDefinition}.
82       *
83       * @return new definition instance
84       * @see RoleDefinition
85       */
86      @Override
87      protected AbstractDefinition newDefinition()
88      {
89          return new RoleDefinition();
90      }
91  
92      /**
93       *
94       */
95      public class RoleDefinition
96          extends AbstractDefinition
97      {
98  
99          /**
100          * Will the access be set or not.
101          */
102         private boolean setAccess = false;
103 
104         @Override
105         protected void readXML(final List<String> _tags,
106                                final Map<String, String> _attributes,
107                                final String _text)
108             throws EFapsException
109         {
110             final String value = _tags.get(0);
111             if ("status".equals(value)) {
112                 addValue("Status", _text);
113             } else if ("global".equals(value)) {
114                 RoleUpdate.this.global = Boolean.valueOf(_text);
115             } else if ("access".equals(value)) {
116                 this.setAccess = true;
117                 if (_tags.size() > 2) {
118                     final String subValue1 = _tags.get(1);
119                     if ("userinterface".equals(subValue1)) {
120                         final String subValue2 = _tags.get(2);
121                         if ("cmd".equals(subValue2)) {
122                             addLink(RoleUpdate.LINK2ACCESSCMD, new LinkInstance(_text));
123                         } else {
124                             super.readXML(_tags, _attributes, _text);
125                         }
126                     } else if ("datamodel".equals(subValue1)) {
127                         final String subValue2 = _tags.get(2);
128                         if ("accessset".equals(subValue2)) {
129                             addLink(RoleUpdate.LINK2ACCESSSET, new LinkInstance(_text));
130                         }
131                     } else {
132                         super.readXML(_tags, _attributes, _text);
133                     }
134                 }
135             } else {
136                 super.readXML(_tags, _attributes, _text);
137             }
138         }
139 
140         @Override
141         protected void setLinksInDB(final Instance _instance,
142                                     final Link _linktype,
143                                     final Set<LinkInstance> _links)
144             throws EFapsException
145         {
146             if (this.setAccess) {
147                 super.setLinksInDB(_instance, _linktype, _links);
148             }
149         }
150     }
151 }