1 /**
2 * Copyright 2011-2013 The Kuali Foundation Licensed under the Educational
3 * Community License, Version 2.0 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of the License
5 * at
6 *
7 * http://www.osedu.org/licenses/ECL-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15 package org.kuali.mobility.admin.util;
16
17 import java.util.Properties;
18
19 import org.kuali.mobility.admin.entity.HomeScreen;
20
21 /**
22 * Utility for checking valid layouts.
23 *
24 * @author Kuali Mobility Team (mobility.dev@kuali.org)
25 * @since 2.3.0
26 */
27 public class LayoutUtil {
28
29 public static final String PROP_LAYOUT_DEFAULT = "home.layout.default";
30
31 public static final String PROP_LAYOUT_EDITABLE = "home.layout.userEditable";
32
33 /**
34 * Returns a valid layout. The returned layout may be the requested layout if it is valid, else
35 * it would return the default layout
36 * @param requestedLayout The layout requested
37 * @param kmeProperties A reference to the KME Properties
38 * @return A valid layout that can be used.
39 */
40 public static final String getValidLayout(String requestedLayout, Properties kmeProperties){
41 String defaultLayout = getDefaultLayout(kmeProperties);
42 return isLayoutAllowed(requestedLayout, kmeProperties) ? requestedLayout : defaultLayout;
43 }
44
45 /**
46 * Returns tre if the layout is an allowed layout
47 * @param layout Layout to check if it is allowed
48 * @param kmeProperties A reference to the KME Properties
49 * @return True if the layout is allowed, else false.
50 */
51 public static final boolean isLayoutAllowed(String layout, Properties kmeProperties){
52 // If the layout name is not one of the defined layouts, it is invalid
53 if(!isValidLayout(layout)){
54 return false;
55 }
56
57 // If layout change is allowed, or if the layout is the default layout
58 if(isLayoutChangeAllowed(kmeProperties) || getDefaultLayout(kmeProperties).equals(layout)){
59 return true;
60 }
61
62 return false;
63 }
64
65 /**
66 * Returns true if users are allowed to change their layout
67 * @param kmeProperties
68 * @return
69 */
70 public static final boolean isLayoutChangeAllowed(Properties kmeProperties){
71 if(kmeProperties == null){
72 return false;
73 }
74 return Boolean.parseBoolean(kmeProperties.getProperty(PROP_LAYOUT_EDITABLE, "false"));
75 }
76
77 /**
78 * Gets the default layout
79 * @param kmeProperties
80 * @return
81 */
82 public static final String getDefaultLayout(Properties kmeProperties){
83 if (kmeProperties == null){
84 return HomeScreen.LAYOUT_LIST;
85 }
86 return kmeProperties.getProperty(PROP_LAYOUT_DEFAULT, HomeScreen.LAYOUT_LIST);
87 }
88
89
90 /**
91 * Returns true uf the specified layout is a valid layout
92 * @param layout Layout name to check
93 * @return True if the layout is valid.
94 */
95 public static final boolean isValidLayout(String layout){
96 if(layout == null){
97 return false;
98 }
99 for(String l : HomeScreen.LAYOUTS){
100 if(l.equals(layout)){
101 return true;
102 }
103 }
104 return false;
105 }
106 }