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