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.admin.ui;
22  
23  import java.util.UUID;
24  
25  import org.efaps.admin.datamodel.Type;
26  import org.efaps.ci.CIAdminUserInterface;
27  import org.efaps.db.Instance;
28  import org.efaps.jaas.AppAccessHandler;
29  import org.efaps.util.EFapsException;
30  import org.efaps.util.cache.CacheReloadException;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  /**
35   * @author The eFaps Team
36   * @version $Id$
37   *
38   */
39  public class Menu
40      extends AbstractMenu
41  {
42  
43      /**
44       * Logging instance used in this class.
45       */
46      protected static final Logger LOG = LoggerFactory.getLogger(Menu.class);
47  
48      /**
49       * Needed for serialization.
50       */
51      private static final long serialVersionUID = 1L;
52  
53      /**
54       * Menu is type menu tree.
55       */
56      private boolean typeMenu = false;
57  
58      /**
59       * Constructor to set the id and name of the menu object.
60       *
61       * @param _id id of the command to set
62       * @param _uuid uuid of this command
63       * @param _name name of the command to set
64       */
65      public Menu(final Long _id,
66                  final String _uuid,
67                  final String _name)
68      {
69          super(_id, _uuid, _name);
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      protected void add(final long _sortId,
77                         final long _id)
78          throws CacheReloadException
79      {
80          final Command command = Command.get(_id);
81          if (command == null) {
82              final Menu subMenu = Menu.get(_id);
83              add(_sortId, subMenu);
84          } else {
85              add(_sortId, command);
86          }
87      }
88  
89      /**
90       * The instance method sets a boolean type menu tree.
91       *
92       * @param _typeMenu boolean to set.
93       */
94      public void setTypeMenu(final boolean _typeMenu)
95      {
96          this.typeMenu = _typeMenu;
97      }
98  
99  
100     /**
101      * This is the getter method for the instance variable {@link #typeMenu}.
102      *
103      * @return value of instance variable {@link #typeMenu}
104      * @see #setTypeMenu
105      * @see #typeMenu
106      */
107     public boolean isTypeMenu()
108     {
109         return this.typeMenu;
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
115     @Override
116     public boolean hasAccess(final TargetMode _targetMode,
117                              final Instance _instance)
118         throws EFapsException
119     {
120         boolean ret = super.hasAccess(_targetMode, _instance);
121 
122         if (!ret && getCommands().size() > 0 && !AppAccessHandler.excludeMode() && isTypeMenu()) {
123             ret = true;
124         }
125         return ret;
126     }
127 
128     /**
129      * Returns for given parameter <i>_id</i> the instance of class {@link Menu}
130      * .
131      *
132      * @param _id id to search in the cache
133      * @return instance of class {@link Menu}
134      * @throws CacheReloadException on error
135      */
136     public static Menu get(final long _id)
137         throws CacheReloadException
138     {
139         return AbstractUserInterfaceObject.<Menu>get(_id, Menu.class, CIAdminUserInterface.Menu.getType());
140     }
141 
142     /**
143      * Returns for given parameter <i>_name</i> the instance of class
144      * {@link Menu}.
145      *
146      * @param _name name to search in the cache
147      * @return instance of class {@link Menu}
148      * @throws CacheReloadException on error
149      */
150     public static Menu get(final String _name)
151         throws CacheReloadException
152     {
153         return AbstractUserInterfaceObject.<Menu>get(_name, Menu.class, CIAdminUserInterface.Menu.getType());
154     }
155 
156     /**
157      * Returns for given parameter <i>UUID</i> the instance of class
158      * {@link Menu}.
159      *
160      * @param _uuid UUID to search in the cache
161      * @return instance of class {@link Menu}
162      * @throws CacheReloadException on error
163      */
164     public static Menu get(final UUID _uuid)
165         throws CacheReloadException
166     {
167         return AbstractUserInterfaceObject.<Menu>get(_uuid, Menu.class, CIAdminUserInterface.Menu.getType());
168     }
169 
170     /**
171      * Returns for given type the type tree menu. If no type tree menu is
172      * defined for the type, it is searched if for parent type a menu is
173      * defined.
174      *
175      * @param _type type for which the type tree menu is searched
176      * @return type tree menu for given type if found; otherwise
177      *         <code>null</code>.
178      * @throws EFapsException on error
179      */
180     public static Menu getTypeTreeMenu(final Type _type)
181         throws EFapsException
182     {
183         return _type.getTypeMenu();
184     }
185 }