View Javadoc
1   package fr.ifremer.quadrige3.core.config;
2   
3   /*
4    * #%L
5    * Tutti :: Persistence
6    * $Id: TuttiConfigurationProvider.java 1418 2013-12-01 21:18:22Z tchemit $
7    * $HeadURL: http://svn.forge.codelutin.com/svn/tutti/trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/TuttiConfigurationProvider.java $
8    * %%
9    * Copyright (C) 2012 - 2013 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import com.google.common.collect.Lists;
27  import fr.ifremer.quadrige3.core.dao.technical.QuadrigeEnumerationDef;
28  import org.nuiton.config.ApplicationConfigProvider;
29  import org.nuiton.config.ConfigActionDef;
30  import org.nuiton.config.ConfigOptionDef;
31  
32  import java.util.List;
33  import java.util.Locale;
34  
35  import static org.nuiton.i18n.I18n.l;
36  
37  /**
38   * Allegro config provider (for site generation).
39   *
40   * @author Benoit Lavenier (benoit.lavenier@e-is.pro)
41   * @since 3.5.0
42   */
43  public class QuadrigeConfigurationEnumProvider implements ApplicationConfigProvider {
44  
45  	/** {@inheritDoc} */
46  	@Override
47  	public String getName() {
48  		return "quadrige3-core-shared-db-enumerations";
49  	}
50  
51  	/** {@inheritDoc} */
52  	@Override
53  	public String getDescription(Locale locale) {
54  		return l(locale, "quadrige3-db-enumerations.config");
55  	}
56  
57  	/** {@inheritDoc} */
58  	@Override
59  	public ConfigOptionDef[] getOptions() {
60  		List<ConfigOptionDef> options = Lists.newArrayList();
61  
62  		// Add options from model enumerations
63  		List<QuadrigeEnumerationDef<?>> enumDefs = QuadrigeEnumerationHelper.getAllModelEnumerations();
64  		for(QuadrigeEnumerationDef<?> enumDef: enumDefs) {
65  			QuadrigeEnumerationOption option = new QuadrigeEnumerationOption(enumDef);
66  			options.add(option);
67  		}
68  		
69  		return options.toArray(new ConfigOptionDef[options.size()]);
70  	}
71  
72  	/** {@inheritDoc} */
73  	@Override
74  	public ConfigActionDef[] getActions() {
75  		return new ConfigActionDef[0];
76  	}	
77  	
78  	static class QuadrigeEnumerationOption implements ConfigOptionDef {
79  
80  	    private static final long serialVersionUID = -5720186783497571417L;
81  	    
82  		private final QuadrigeEnumerationDef<?> delegatedEnum;
83  		
84  		public QuadrigeEnumerationOption(QuadrigeEnumerationDef<?> delegatedEnum) {
85  			this.delegatedEnum = delegatedEnum;
86  		}
87  
88  		@Override
89  		public boolean isFinal() {
90  			return false;
91  		}
92  
93  		@Override
94  		public boolean isTransient() {
95  			return false;
96  		}
97  
98  		@Override
99  		public String getDefaultValue() {
100 			return delegatedEnum.getValueAsString();
101 		}
102 
103 		@Override
104 		public String getDescription() {
105 			return delegatedEnum.getDescription();
106 		}
107 
108 		@Override
109 		public String getKey() {
110 			return delegatedEnum.getKey();
111 		}
112 
113 		@SuppressWarnings({ "unchecked", "rawtypes" })
114         @Override
115 		public Class getType() {
116 			return delegatedEnum.getType();
117 		}
118 
119 		@Override
120 		public void setDefaultValue(String defaultValue) {
121 			//not used
122 		}
123 
124 		@Override
125 		public void setTransient(boolean isTransient) {
126 			// not used
127 		}
128 
129 		@Override
130 		public void setFinal(boolean isFinal) {
131 			// not used
132 		}
133 	}
134 }