1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.view;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.log4j.Logger;
20 import org.kuali.rice.core.api.CoreApiServiceLocator;
21 import org.kuali.rice.core.api.config.property.ConfigurationService;
22 import org.kuali.rice.krad.datadictionary.parse.BeanTag;
23 import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
24 import org.kuali.rice.krad.datadictionary.parse.BeanTags;
25 import org.kuali.rice.krad.datadictionary.uif.UifDictionaryBeanBase;
26 import org.kuali.rice.krad.uif.UifConstants;
27 import org.kuali.rice.krad.util.KRADConstants;
28
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.Serializable;
32 import java.net.URL;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Properties;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 @BeanTags({@BeanTag(name = "viewTheme-bean", parent = "Uif-ViewTheme"),
69 @BeanTag(name = "kbootTheme-bean", parent = "Uif-KbootTheme")})
70 public class ViewTheme extends UifDictionaryBeanBase implements Serializable {
71 private static final long serialVersionUID = 7063256242857896580L;
72 private static final Logger LOG = Logger.getLogger(ViewTheme.class);
73
74 private String name;
75
76 private String directory;
77 private String imageDirectory;
78
79 private String minVersionSuffix;
80 private String minFileName;
81 private boolean includeMinFiles;
82 private List<String> minCssSourceFiles;
83 private List<String> minScriptSourceFiles;
84
85 private List<String> cssFiles;
86 private List<String> scriptFiles;
87
88 private boolean usesThemeBuilder;
89
90 public ViewTheme() {
91 super();
92
93 this.includeMinFiles = true;
94 this.minCssSourceFiles = new ArrayList<String>();
95 this.minScriptSourceFiles = new ArrayList<String>();
96
97 this.cssFiles = new ArrayList<String>();
98 this.scriptFiles = new ArrayList<String>();
99
100 this.usesThemeBuilder = true;
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115 public void configureThemeDefaults() {
116 boolean inDevMode = getConfigurationService().getPropertyValueAsBoolean(
117 KRADConstants.ConfigParameters.KRAD_DEV_MODE);
118
119
120 if (inDevMode) {
121 if (this.usesThemeBuilder) {
122 setMinFileLists();
123 }
124
125 this.cssFiles.addAll(0, this.minCssSourceFiles);
126 this.scriptFiles.addAll(0, this.minScriptSourceFiles);
127 }
128
129
130 else if (this.includeMinFiles) {
131 if (StringUtils.isBlank(this.minFileName)) {
132 if (StringUtils.isBlank(this.minVersionSuffix)) {
133 this.minVersionSuffix = getConfigurationService().getPropertyValueAsString(
134 KRADConstants.ConfigParameters.APPLICATION_VERSION);
135 }
136
137 this.minFileName = this.name + "." + this.minVersionSuffix + UifConstants.FileExtensions.MIN;
138 }
139
140 String themeDirectory = getThemeDirectory();
141
142 this.cssFiles.add(0,
143 themeDirectory + UifConstants.DEFAULT_STYLESHEETS_DIRECTORY + "/" + this.minFileName
144 + UifConstants.FileExtensions.CSS);
145 this.scriptFiles.add(0,
146 themeDirectory + UifConstants.DEFAULT_SCRIPTS_DIRECTORY + "/" + this.minFileName
147 + UifConstants.FileExtensions.JS);
148 }
149 }
150
151
152
153
154
155
156
157
158
159
160
161 public String getThemeDirectory() {
162 String themeDirectory = "";
163
164 if (StringUtils.isNotBlank(this.directory)) {
165 if (this.directory.startsWith("/")) {
166 this.directory = this.directory.substring(1);
167 }
168
169 themeDirectory = this.directory;
170 } else {
171 themeDirectory = UifConstants.DEFAULT_THEMES_DIRECTORY.substring(1) + "/" + this.name + "/";
172 }
173
174 return themeDirectory;
175 }
176
177
178
179
180
181 protected void setMinFileLists() {
182 Properties themeProperties = null;
183 try {
184 themeProperties = getThemeProperties();
185 } catch (IOException e) {
186 throw new RuntimeException("Unable to retrieve theme properties for theme: " + this.name);
187 }
188
189 if (themeProperties == null) {
190 LOG.warn("No theme properties file found for theme with name: " + this.name);
191
192 return;
193 }
194
195 String[] cssFiles = getPropertyValue(themeProperties, UifConstants.THEME_CSS_FILES);
196
197 if (cssFiles != null) {
198 for (String cssFile : cssFiles) {
199 this.minCssSourceFiles.add(cssFile);
200 }
201 }
202
203 String[] jsFiles = getPropertyValue(themeProperties, UifConstants.THEME_JS_FILES);
204
205 if (jsFiles != null) {
206 for (String jsFile : jsFiles) {
207 this.minScriptSourceFiles.add(jsFile);
208 }
209 }
210 }
211
212
213
214
215
216
217
218
219
220
221
222
223 protected Properties getThemeProperties() throws IOException {
224 Properties themeProperties = null;
225
226 String appUrl = getConfigurationService().getPropertyValueAsString(KRADConstants.ConfigParameters.APPLICATION_URL);
227 String propertiesUrlPath = appUrl + "/" + getThemeDirectory() + "/"
228 + UifConstants.THEME_DERIVED_PROPERTY_FILE;
229
230 InputStream inputStream = null;
231 try {
232 URL propertiesUrl = new URL(propertiesUrlPath);
233 inputStream = propertiesUrl.openStream();
234
235 themeProperties = new Properties();
236 themeProperties.load(inputStream);
237 } finally {
238 if (inputStream != null) {
239 inputStream.close();
240 }
241 }
242
243 return themeProperties;
244 }
245
246
247
248
249
250
251
252
253
254 protected String[] getPropertyValue(Properties properties, String key) {
255 String[] propertyValueArray = null;
256
257 if (properties.containsKey(key)) {
258 String propertyValueString = properties.getProperty(key);
259
260 if (propertyValueString != null) {
261 propertyValueArray = propertyValueString.split(",");
262 }
263 }
264
265 return propertyValueArray;
266 }
267
268
269
270
271
272
273
274
275
276
277
278 @BeanTagAttribute(name = "name")
279 public String getName() {
280 return name;
281 }
282
283
284
285
286
287
288 public void setName(String name) {
289 this.name = name;
290 }
291
292
293
294
295
296
297
298
299
300
301 @BeanTagAttribute(name = "directory")
302 public String getDirectory() {
303 return directory;
304 }
305
306
307
308
309
310
311 public void setDirectory(String directory) {
312 this.directory = directory;
313 }
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330 @BeanTagAttribute(name = "imageDirectory")
331 public String getImageDirectory() {
332 if (StringUtils.isBlank(this.imageDirectory)) {
333 String appUrl = getConfigurationService().getPropertyValueAsString(KRADConstants.ConfigParameters.APPLICATION_URL);
334
335 this.imageDirectory = appUrl + "/" + getThemeDirectory() + UifConstants.DEFAULT_IMAGES_DIRECTORY + "/";
336 }
337
338 return imageDirectory;
339 }
340
341
342
343
344
345
346 public void setImageDirectory(String imageDirectory) {
347 this.imageDirectory = imageDirectory;
348 }
349
350
351
352
353
354
355
356
357
358
359
360 @BeanTagAttribute(name = "minVersionSuffix")
361 public String getMinVersionSuffix() {
362 return minVersionSuffix;
363 }
364
365
366
367
368
369
370 public void setMinVersionSuffix(String minVersionSuffix) {
371 this.minVersionSuffix = minVersionSuffix;
372 }
373
374
375
376
377
378
379
380
381
382
383
384
385
386 @BeanTagAttribute(name = "minFileName")
387 public String getMinFileName() {
388 return minFileName;
389 }
390
391
392
393
394
395
396 public void setMinFileName(String minFileName) {
397 this.minFileName = minFileName;
398 }
399
400
401
402
403
404
405
406
407
408
409
410 public boolean isIncludeMinFiles() {
411 return includeMinFiles;
412 }
413
414
415
416
417
418
419 public void setIncludeMinFiles(boolean includeMinFiles) {
420 this.includeMinFiles = includeMinFiles;
421 }
422
423
424
425
426
427
428
429
430
431
432
433
434 @BeanTagAttribute(name = "minCssSourceFiles", type = BeanTagAttribute.AttributeType.LISTVALUE)
435 public List<String> getMinCssSourceFiles() {
436 return minCssSourceFiles;
437 }
438
439
440
441
442
443
444 public void setMinCssSourceFiles(List<String> minCssSourceFiles) {
445 this.minCssSourceFiles = minCssSourceFiles;
446 }
447
448
449
450
451
452
453
454
455
456
457
458
459 @BeanTagAttribute(name = "minScriptSourceFiles", type = BeanTagAttribute.AttributeType.LISTVALUE)
460 public List<String> getMinScriptSourceFiles() {
461 return minScriptSourceFiles;
462 }
463
464
465
466
467
468
469 public void setMinScriptSourceFiles(List<String> minScriptSourceFiles) {
470 this.minScriptSourceFiles = minScriptSourceFiles;
471 }
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489 @BeanTagAttribute(name = "cssFiles", type = BeanTagAttribute.AttributeType.LISTVALUE)
490 public List<String> getCssFiles() {
491 return cssFiles;
492 }
493
494
495
496
497
498
499 public void setCssFiles(List<String> cssFiles) {
500 this.cssFiles = cssFiles;
501 }
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519 @BeanTagAttribute(name = "scriptFiles", type = BeanTagAttribute.AttributeType.LISTVALUE)
520 public List<String> getScriptFiles() {
521 return scriptFiles;
522 }
523
524
525
526
527
528
529 public void setScriptFiles(List<String> scriptFiles) {
530 this.scriptFiles = scriptFiles;
531 }
532
533
534
535
536
537
538
539
540
541
542
543
544
545 @BeanTagAttribute(name = "usesThemeBuilder")
546 public boolean isUsesThemeBuilder() {
547 return usesThemeBuilder;
548 }
549
550
551
552
553
554
555 public void setUsesThemeBuilder(boolean usesThemeBuilder) {
556 this.usesThemeBuilder = usesThemeBuilder;
557 }
558
559
560
561
562
563
564 public ConfigurationService getConfigurationService() {
565 return CoreApiServiceLocator.getKualiConfigurationService();
566 }
567
568
569
570
571
572
573 public <T> T copy() {
574 T copiedClass = null;
575 try {
576 copiedClass = (T) this.getClass().newInstance();
577 } catch (Exception exception) {
578 throw new RuntimeException();
579 }
580
581 copyProperties(copiedClass);
582
583 return copiedClass;
584 }
585
586
587
588
589
590
591 protected <T> void copyProperties(T viewTheme) {
592 super.copyProperties(viewTheme);
593
594 ViewTheme viewThemeCopy = (ViewTheme) viewTheme;
595
596 viewThemeCopy.setName(this.name);
597 viewThemeCopy.setDirectory(this.directory);
598 viewThemeCopy.setImageDirectory(this.imageDirectory);
599 viewThemeCopy.setMinVersionSuffix(this.minVersionSuffix);
600 viewThemeCopy.setMinFileName(this.minFileName);
601 viewThemeCopy.setIncludeMinFiles(this.includeMinFiles);
602
603 if (this.minCssSourceFiles != null) {
604 viewThemeCopy.setMinCssSourceFiles(new ArrayList<String>(this.minCssSourceFiles));
605 }
606
607 if (this.minScriptSourceFiles != null) {
608 viewThemeCopy.setMinScriptSourceFiles(new ArrayList<String>(this.minScriptSourceFiles));
609 }
610
611 if (this.cssFiles != null) {
612 viewThemeCopy.setCssFiles(new ArrayList<String>(this.cssFiles));
613 }
614
615 if (this.scriptFiles != null) {
616 viewThemeCopy.setScriptFiles(new ArrayList<String>(this.scriptFiles));
617 }
618
619 viewThemeCopy.setUsesThemeBuilder(this.usesThemeBuilder);
620 }
621 }