001/* 002 * Copyright 2012 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.core.userpref; 017 018import java.util.HashMap; 019import java.util.Map; 020 021import org.ametys.runtime.i18n.I18nizableText; 022import org.ametys.runtime.model.DefaultElementDefinition; 023import org.ametys.runtime.model.DefinitionContext; 024 025/** 026 * Definition of a user preference. 027 * @param <T> Type of the preference value 028 */ 029public class UserPreference<T> extends DefaultElementDefinition<T> 030{ 031 /** The storage manager role */ 032 protected String _managerRole; 033 034 /** The private status. */ 035 protected boolean _private; 036 037 /** the group in which to display the element */ 038 protected I18nizableText _displayGroup; 039 040 /** the position in the group where the element has to be displayed */ 041 protected long _position; 042 043 /** 044 * Default constructor 045 */ 046 public UserPreference() 047 { 048 super(); 049 } 050 051 /** 052 * Constructor by copying an existing {@link UserPreference}. 053 * @param preferenceToCopy The {@link UserPreference} to copy 054 */ 055 public UserPreference(UserPreference<T> preferenceToCopy) 056 { 057 super(preferenceToCopy); 058 059 setManagerRole(preferenceToCopy.getManagerRole()); 060 setPrivate(preferenceToCopy.isPrivate()); 061 062 setDisplayGroup(preferenceToCopy.getDisplayGroup()); 063 setPosition(preferenceToCopy.getPosition()); 064 } 065 066 /** 067 * Get the storage manager role. 068 * @return the manager role. Can be <code>null</code> to use the default storage manager. 069 */ 070 public String getManagerRole() 071 { 072 return _managerRole; 073 } 074 075 /** 076 * Set the storage manager role. 077 * @param managerRole the manager role to set. Can be <code>null</code> to use the default storage manager. 078 */ 079 public void setManagerRole(String managerRole) 080 { 081 _managerRole = managerRole; 082 } 083 084 /** 085 * Get whether the preference is private, i.e. should not be visible by 086 * the regular user preferences interface. 087 * @return <code>true</code> if the preference is private, <code>false</code> if it is public. 088 */ 089 public boolean isPrivate() 090 { 091 return _private; 092 } 093 094 /** 095 * Set the private status of the preference 096 * @param privateStatus <code>true</code> if the preference is private, <code>false</code> if it is public. 097 */ 098 public void setPrivate(boolean privateStatus) 099 { 100 _private = privateStatus; 101 } 102 103 /** 104 * Retrieves the group in which to display the preference 105 * @return the group 106 */ 107 public I18nizableText getDisplayGroup() 108 { 109 return _displayGroup; 110 } 111 112 /** 113 * Sets the group in which to display the preference 114 * @param displayGroup the group to set 115 */ 116 public void setDisplayGroup(I18nizableText displayGroup) 117 { 118 this._displayGroup = displayGroup; 119 } 120 121 /** 122 * Retrieves the position in the group where the preference has to be displayed 123 * @return the position in the group 124 */ 125 public long getPosition() 126 { 127 return _position; 128 } 129 130 /** 131 * Sets the position of the preference in the group 132 * @param position the position to set 133 */ 134 public void setPosition(long position) 135 { 136 this._position = position; 137 } 138 139 @Override 140 protected Map<String, Object> _toJSON(DefinitionContext context) 141 { 142 Map<String, Object> result = new HashMap<>(super._toJSON(context)); 143 result.put("private", isPrivate()); 144 return result; 145 } 146 147 @Override 148 public String toString() 149 { 150 return "Preference '" + getName() + "' (type: " + (isMultiple() ? "multiple " : " ") + getType().getId() + "', private: " + _private + ", label: " + getLabel().toString() + ", " + (getDefaultValue() != null ? "default value: " + getDefaultValue() : "no default value") + ", " + (getEnumerator() != null ? "enumerator: " + getEnumerator() : "no enumerator") + ")"; 151 } 152}