View Javadoc

1   /*
2   Copyright 2010 James Pether Sörling Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 
3   	$Id
4   */
5   
6   package com.hack23.cia.model.application.impl.common;
7   
8   import gnu.trove.THashSet;
9   
10  import java.util.Set;
11  
12  import javax.persistence.CascadeType;
13  import javax.persistence.Entity;
14  import javax.persistence.FetchType;
15  import javax.persistence.GeneratedValue;
16  import javax.persistence.GenerationType;
17  import javax.persistence.Id;
18  import javax.persistence.JoinTable;
19  import javax.persistence.OneToMany;
20  import javax.persistence.Transient;
21  import javax.persistence.Version;
22  
23  import org.hibernate.annotations.Cache;
24  import org.hibernate.annotations.CacheConcurrencyStrategy;
25  
26  import com.hack23.cia.model.core.impl.BaseEntity;
27  
28  /***
29   * The Class Agency.
30   */
31  @Entity
32  @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
33  public class Agency extends BaseEntity {
34  
35      /***
36       * The Enum LanguageContentKey.
37       */
38      public enum LanguageContentKey {
39          
40          /*** The ABSENCE. */
41  		 ABSENCE, 
42   
43   /*** The ABSENC e_ percentage. */
44   ABSENCE_PERCENTAGE, 
45   
46   /*** The ABSENT. */
47    ABSENT, 
48   
49   /*** The ABSEN t_ occassions. */
50   ABSENT_OCCASSIONS, 
51   
52   /*** The ACTIV e_ b y_ default. */
53   ACTIVE_BY_DEFAULT, 
54   
55   /*** The AD d_ language. */
56   ADD_LANGUAGE, 
57   
58   /*** The ADMI n_ menu. */
59   ADMIN_MENU, 
60   
61   /*** The AGREEMENTS. */
62   AGREEMENTS, 
63    
64   /*** The AGREMEN t_ percentage. */
65   AGREMENT_PERCENTAGE, 
66   
67   /*** The ALLOWANCES. */
68   ALLOWANCES, 
69   
70   /*** The BALLOT. */
71   BALLOT, 
72   
73   /*** The BALLOTS. */
74   BALLOTS, 
75   
76   /*** The BUTTO n_ cancel. */
77   BUTTON_CANCEL, 
78   
79   /*** The BUTTO n_ delete. */
80   BUTTON_DELETE, 
81   
82   /*** The BUTTO n_ login. */
83   BUTTON_LOGIN, 
84   
85   /*** The BUTTO n_ ne w_ portal. */
86   BUTTON_NEW_PORTAL, 
87   
88   /*** The BUTTO n_ ok. */
89   BUTTON_OK, 
90   
91   /*** The CATEGORY. */
92   CATEGORY, 
93   
94   /*** The CHARTS. */
95   CHARTS, 
96   
97   /*** The CITIZE n_ operation. */
98   CITIZEN_OPERATION, 
99   
100  /*** The COMMITE e_ report. */
101  COMMITEE_REPORT, 
102  
103  /*** The CONFIGUR e_ agency. */
104  CONFIGURE_AGENCY, 
105  
106  /*** The CONFIGUR e_ language. */
107  CONFIGURE_LANGUAGE, 
108  
109  /*** The CONFIGUR e_ portal. */
110  CONFIGURE_PORTAL, 
111  
112  /*** The CURREN t_ action. */
113  CURRENT_ACTION, 
114  
115  /*** The DECISIO n_ date. */
116  DECISION_DATE, 
117  
118  /*** The DONAT e_ t o_ project. */
119  DONATE_TO_PROJECT, 
120  
121  /*** The ELECTORA l_ area. */
122  ELECTORAL_AREA, 
123  
124  /*** The ERRO r_ message. */
125  ERROR_MESSAGE, 
126  
127  /*** The FIRS t_ vote. */
128  FIRST_VOTE, 
129  
130  /*** The FORUM. */
131  FORUM, 
132  
133  /*** The FRIEND s_ an d_ enemies. */
134  FRIENDS_AND_ENEMIES, 
135  
136  /*** The GRADING. */
137  GRADING, 
138  
139  /*** The HOM e_ page. */
140  HOME_PAGE, 
141  
142  /*** The HOUSES. */
143  HOUSES, 
144  
145  /*** The INCOM e_ activity. */
146  INCOME_ACTIVITY, 
147  
148  /*** The INFORMATION. */
149  INFORMATION, 
150  
151  /*** The INSID e_ party. */
152  INSIDE_PARTY, 
153  
154  /*** The LANGUAG e_ conten t_ key. */
155  LANGUAGE_CONTENT_KEY, 
156  
157  /*** The LANGUAG e_ conten t_ value. */
158  LANGUAGE_CONTENT_VALUE, 
159  
160  /*** The LANGUAG e_ shortcode. */
161  LANGUAGE_SHORTCODE, 
162  
163  /*** The LAS t_ te n_ decide d_ commite e_ reports. */
164  LAST_TEN_DECIDED_COMMITEE_REPORTS, 
165  
166  /*** The LAS t_ te n_ decide d_ commite e_ report s_ description. */
167  LAST_TEN_DECIDED_COMMITEE_REPORTS_DESCRIPTION, 
168  
169  /*** The LAS t_ vote. */
170  LAST_VOTE, 
171  
172  /*** The LOGIN. */
173  LOGIN, 
174  
175  /*** The LOGOUT. */
176  LOGOUT, 
177  
178  /*** The LOSE r_ percent. */
179  LOSER_PERCENT, 
180  
181  /*** The LOSERS. */
182  LOSERS, 
183  
184  /*** The LOST. */
185  LOST, 
186  
187  /*** The LOYAL. */
188  LOYAL, 
189  
190  /*** The LOYA l_ occassions. */
191  LOYAL_OCCASSIONS, 
192  
193  /*** The LOYA l_ percent. */
194  LOYAL_PERCENT, 
195  
196  /*** The LOYA l_ percentag e_ b y_ party. */
197  LOYAL_PERCENTAGE_BY_PARTY, 
198  
199  /*** The LOYALTIES. */
200  LOYALTIES, 
201  
202  /*** The MATCHE s_ url. */
203  MATCHES_URL, 
204  
205  /*** The NAME. */
206  NAME, 
207  
208  /*** The NEUTRAL. */
209  NEUTRAL, 
210  
211  /*** The NO. */
212  NO, 
213  
214  /*** The N o_ registe r_ information. */
215  NO_REGISTER_INFORMATION, 
216  
217  /*** The N o_ registe r_ informatio n_ msg. */
218  NO_REGISTER_INFORMATION_MSG, 
219  
220  /*** The NOTHIN g_ declared. */
221  NOTHING_DECLARED, 
222  
223  /*** The OPINIONS. */
224  OPINIONS, 
225  
226  /*** The OTHE r_ loyalties. */
227  OTHER_LOYALTIES, 
228  
229  /*** The OUTCOME. */
230  OUTCOME, 
231  
232  /*** The PARLIAMEN t_ member s_ absence. */
233  PARLIAMENT_MEMBERS_ABSENCE, 
234  
235  /*** The PARLIAMEN t_ member s_ absenc e_ description. */
236  PARLIAMENT_MEMBERS_ABSENCE_DESCRIPTION, 
237  
238  /*** The PARLIAMEN t_ member s_ losers. */
239  PARLIAMENT_MEMBERS_LOSERS, 
240  
241  /*** The PARLIAMEN t_ member s_ loser s_ description. */
242  PARLIAMENT_MEMBERS_LOSERS_DESCRIPTION, 
243  
244  /*** The PARLIAMEN t_ member s_ loyal. */
245  PARLIAMENT_MEMBERS_LOYAL, 
246  
247  /*** The PARLIAMEN t_ member s_ loya l_ description. */
248  PARLIAMENT_MEMBERS_LOYAL_DESCRIPTION, 
249  
250  /*** The PARLIAMEN t_ member s_ presence. */
251  PARLIAMENT_MEMBERS_PRESENCE, 
252  
253  /*** The PARLIAMEN t_ member s_ presenc e_ description. */
254  PARLIAMENT_MEMBERS_PRESENCE_DESCRIPTION, 
255  
256  /*** The PARLIAMEN t_ member s_ rebels. */
257  PARLIAMENT_MEMBERS_REBELS, 
258  
259  /*** The PARLIAMEN t_ member s_ rebel s_ description. */
260  PARLIAMENT_MEMBERS_REBELS_DESCRIPTION, 
261  
262  /*** The PARLIAMEN t_ member s_ winners. */
263  PARLIAMENT_MEMBERS_WINNERS, 
264  
265  /*** The PARLIAMEN t_ member s_ winner s_ description. */
266  PARLIAMENT_MEMBERS_WINNERS_DESCRIPTION, 
267  
268  /*** The PARTY. */
269  PARTY, 
270  
271  /*** The PART y_ loyal. */
272  PARTY_LOYAL, 
273  
274  /*** The PART y_ rebel. */
275  PARTY_REBEL, 
276  
277  /*** The PART y_ status. */
278  PARTY_STATUS, 
279  
280  /*** The PASSWORD. */
281  PASSWORD, 
282  
283  /*** The PE r_ party. */
284  PER_PARTY, 
285  
286  /*** The POLITICA l_ part y_ behavior. */
287  POLITICAL_PARTY_BEHAVIOR, 
288  
289  /*** The PORTALS. */
290  PORTALS, 
291  
292  /*** The PRESENCE. */
293  PRESENCE, 
294  
295  /*** The PRESENC e_ occassions. */
296  PRESENCE_OCCASSIONS, 
297  
298  /*** The PRESENC e_ percentage. */
299  PRESENCE_PERCENTAGE, 
300  
301  /*** The PRESEN t_ percentag e_ b y_ party. */
302  PRESENT_PERCENTAGE_BY_PARTY, 
303  
304  /*** The PROFFESSIONA l_ behavior. */
305  PROFFESSIONAL_BEHAVIOR, 
306  
307  /*** The PROJEC t_ information. */
308  PROJECT_INFORMATION, 
309  
310  /*** The PUBLI c_ roles. */
311  PUBLIC_ROLES, 
312  
313  /*** The RANK. */
314  RANK, 
315  
316  /*** The REBE l_ occassions. */
317  REBEL_OCCASSIONS, 
318  
319  /*** The REBE l_ percentage. */
320  REBEL_PERCENTAGE, 
321  
322  /*** The REBELS. */
323  REBELS, 
324  
325  /*** The REGISTERE d_ votes. */
326  REGISTERED_VOTES, 
327  
328  /*** The RIKSDA g_ information. */
329  RIKSDAG_INFORMATION, 
330  
331  /*** The ROLES. */
332  ROLES, 
333  
334  /*** The SEARCH. */
335  SEARCH, 
336  
337  /*** The SEARC h_ description. */
338  SEARCH_DESCRIPTION, 
339  
340  /*** The SEARC h_ result. */
341  SEARCH_RESULT, 
342  
343  /*** The SHOR t_ comment. */
344  SHORT_COMMENT, 
345  
346  /*** The SHO w_ recen t_ actions. */
347  SHOW_RECENT_ACTIONS, 
348  
349  /*** The SHO w_ respons e_ time. */
350  SHOW_RESPONSE_TIME, 
351  
352  /*** The SHO w_ top. */
353  SHOW_TOP, 
354  
355  /*** The SHO w_ use r_ history. */
356  SHOW_USER_HISTORY, 
357  
358  /*** The SIT e_ description. */
359  SITE_DESCRIPTION, 
360  
361  /*** The STAR t_ updat e_ job. */
362  START_UPDATE_JOB, 
363  
364  /*** The STOCKS. */
365  STOCKS, 
366  
367  /*** The SUMMARY. */
368 SUMMARY, 
369  
370  /*** The SWEDE n_ arbetarepartie t_ socialdemokraterna. */
371  SWEDEN_ARBETAREPARTIET_SOCIALDEMOKRATERNA, 
372  
373  /*** The SWEDE n_ arbetarepartie t_ socialdemokratern a_ shortcode. */
374  SWEDEN_ARBETAREPARTIET_SOCIALDEMOKRATERNA_SHORTCODE, 
375  
376  /*** The SWEDE n_ centerpartiet. */
377  SWEDEN_CENTERPARTIET, 
378  
379  /*** The SWEDE n_ centerpartie t_ shortcode. */
380  SWEDEN_CENTERPARTIET_SHORTCODE, 
381  
382  /*** The SWEDE n_ folkpartie t_ liberalerna. */
383  SWEDEN_FOLKPARTIET_LIBERALERNA, 
384  
385  /*** The SWEDE n_ folkpartie t_ liberalern a_ shortcode. */
386  SWEDEN_FOLKPARTIET_LIBERALERNA_SHORTCODE, 
387  
388  /*** The SWEDE n_ kristdemokraterna. */
389  SWEDEN_KRISTDEMOKRATERNA, 
390  
391  /*** The SWEDE n_ kristdemokratern a_ shortcode. */
392  SWEDEN_KRISTDEMOKRATERNA_SHORTCODE, 
393  
394  /*** The SWEDE n_ milj o_ partie t_ d e_ grona. */
395  SWEDEN_MILJO_PARTIET_DE_GRONA, 
396  
397  /*** The SWEDE n_ milj o_ partie t_ d e_ gron a_ shortcode. */
398  SWEDEN_MILJO_PARTIET_DE_GRONA_SHORTCODE, 
399  
400  /*** The SWEDE n_ moderat a_ samlingspartiet. */
401  SWEDEN_MODERATA_SAMLINGSPARTIET, 
402  
403  /*** The SWEDE n_ moderat a_ samlingspartie t_ shortcode. */
404  SWEDEN_MODERATA_SAMLINGSPARTIET_SHORTCODE,  
405   
406   /*** The SWEDE n_ vanste r_ partiet. */
407  SWEDEN_VANSTER_PARTIET,
408 
409  /*** The SWEDE n_ vanste r_ partie t_ shortcode. */
410  SWEDEN_VANSTER_PARTIET_SHORTCODE,
411 
412  /*** The SWEDIS h_ parliamen t_ menu. */
413  SWEDISH_PARLIAMENT_MENU,
414 
415  /*** The TO p_ bes t_ friends. */
416  TOP_BEST_FRIENDS, 
417  
418  /*** The TO p_ wors t_ enemies. */
419  TOP_WORST_ENEMIES, 
420  
421  /*** The TRACKIN g_ politician s_ lik e_ bugs. */
422  TRACKING_POLITICIANS_LIKE_BUGS, 
423  
424  /*** The USAG e_ order. */
425  USAGE_ORDER, 
426  
427  /*** The USE r_ history. */
428  USER_HISTORY, 
429  
430  /*** The VOTE. */
431  VOTE, 
432  
433  /*** The VOT e_ occassion s_ compared. */
434  VOTE_OCCASSIONS_COMPARED, 
435  
436  /*** The VOT e_ occassion s_ lost. */
437  VOTE_OCCASSIONS_LOST, 
438  
439  /*** The VOT e_ position. */
440  VOTE_POSITION, 
441  
442  /*** The VOTES. */
443  VOTES, 
444  
445  /*** The WIKI. */
446  WIKI, 
447  
448  /*** The WIKILEAKS. */
449  WIKILEAKS, 
450  
451  /*** The WINNERS. */
452  WINNERS, 
453  
454  /*** The WINNIN g_ occassions. */
455  WINNING_OCCASSIONS, 
456  
457  /*** The WINNIN g_ percentage. */
458  WINNING_PERCENTAGE, 
459  
460  /*** The WINNIN g_ percentag e_ b y_ party. */
461  WINNING_PERCENTAGE_BY_PARTY, 
462  
463  /*** The WINNIN g_ side. */
464  WINNING_SIDE,
465 
466 /*** The WINNIN g_ sid e_ i n_ party. */
467  WINNING_SIDE_IN_PARTY, 
468  
469  /*** The WINNIN g_ status. */
470  WINNING_STATUS, 
471  
472  /*** The WON. */
473  WON, 
474  
475  /*** The YES. */
476  YES      
477     }
478     
479     /*** The Constant serialVersionUID. */
480     private static final long serialVersionUID = 1L;
481 
482     /*** The id. */
483     private Long id;
484 
485     /*** The language content. */
486     private Set<LanguageContent> languageContent = new THashSet<LanguageContent>();
487 
488     
489     /*** The languages. */
490     private Set<Language> languages = new THashSet<Language>();
491 
492     /*** The name. */
493     private String name;
494 
495     /*** The portals. */
496     private Set<Portal> portals = new THashSet<Portal>();
497 
498     /*** The version. */
499     private Long version=1L;
500 
501     /***
502      * Find language.
503      *
504      * @param languageId the language id
505      * @return the language
506      */
507     @Transient
508     public Language findLanguage(final Long languageId) {
509         for (final Language language : languages) {
510             if (languageId.equals(language.getId())) {
511                 return language;
512             }
513         }
514         return null;
515     }
516 
517     /***
518      * Find language content.
519      *
520      * @param languageContentId the language content id
521      * @return the language content
522      */
523     @Transient
524     public LanguageContent findLanguageContent(final Long languageContentId) {
525         for (final LanguageContent languageContent : getLanguageContent()) {
526             if (languageContentId.equals(languageContent.getId())) {
527                 return languageContent;
528             }
529         }
530         return null;
531    }
532 
533     /***
534      * Find matching language.
535      *
536      * @param acceptLanguagesCsv the accept languages csv
537      * @return the language
538      */
539     @Transient
540     public Language findMatchingLanguage(final String acceptLanguagesCsv) {
541         Language lowestMatched = null;
542         
543     	if (acceptLanguagesCsv != null) {
544             for (final Language language : getLanguages()) {
545                 if (language.getActiveByDefault() || acceptLanguagesCsv.toLowerCase().contains(language.getShortCode().toLowerCase())) {
546                     
547                 	if (lowestMatched != null) {
548                 		if (language.getUsageOrder() < lowestMatched.getUsageOrder()) {
549                 			lowestMatched = language;
550                 		}                		
551                 	} else {
552                 		lowestMatched = language;
553                 	}
554                 }
555             }
556         }
557         return lowestMatched;
558     }
559 
560     /***
561      * Find matching portal.
562      *
563      * @param headerHost the header host
564      * @return the portal
565      */
566     @Transient
567     public Portal findMatchingPortal(final String headerHost) {
568     	Portal lowestMatched = null;
569     	
570         if (headerHost != null) {
571             for (final Portal portal : portals) {
572                 if (portal.getActiveByDefault() || headerHost.toLowerCase().contains(
573                         portal.getMatchesUrl().toLowerCase())) {
574                 	if (lowestMatched != null) {
575                 		if (portal.getUsageOrder() < lowestMatched.getUsageOrder()) {
576                 			lowestMatched = portal;
577                 		}                		
578                 	} else {
579                 		lowestMatched = portal;
580                 	}
581                 }
582             }
583         }
584         return lowestMatched;
585     }
586 
587     /*
588      * (non-Javadoc)
589      * 
590      * @see com.hack23.cia.model.core.BaseEntity#getId()
591      */
592     @Override
593     @Id
594     @GeneratedValue(strategy = GenerationType.AUTO)
595     public Long getId() {
596         return id;
597     }
598 
599     /***
600      * Gets the language content.
601      *
602      * @return the language content
603      */
604     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
605     @JoinTable(name = "AgencyLanguageContent")
606     @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
607     public Set<LanguageContent> getLanguageContent() {
608         return languageContent;
609     }
610 
611     /***
612      * Gets the language content by key.
613      *
614      * @param key the key
615      * @param language the language
616      * @return the language content by key
617      */
618     @Transient
619     public LanguageContent getLanguageContentByKey(final Agency.LanguageContentKey key,final Language language) {
620         
621         for (final LanguageContent languageContent :getLanguageContent()) {
622             if (key.equals(LanguageContentKey.valueOf(languageContent.getContentPropertyName())) && language.getId().equals(languageContent.getLanguage().getId())) {
623                 return languageContent;
624             }
625             
626         }
627         return null;
628     }
629     
630     /***
631      * Gets the language content by key.
632      *
633      * @param key the key
634      * @param language the language
635      * @return the language content by key
636      */
637     @Transient
638     public LanguageContent getLanguageContentByKey(final String key,final Language language) {
639         
640         for (final LanguageContent languageContent :getLanguageContent()) {
641             if (key.equals(languageContent.getContentPropertyName()) && language.getId().equals(languageContent.getLanguage().getId())) {
642                 return languageContent;
643             }
644             
645         }
646         return null;
647     }
648     
649     /***
650      * Gets the language content by language.
651      *
652      * @param language the language
653      * @return the language content by language
654      */
655     @Transient
656     public Set<LanguageContent> getLanguageContentByLanguage(final Language language) {
657         final Set<LanguageContent> languageSpecificContent= new THashSet<LanguageContent>();
658         
659         if (language != null) {
660         	
661 	        for (final LanguageContent languageContent :getLanguageContent()) {
662 	        	if (languageContent.getLanguage() != null) {
663 	        		if (language.getId().equals(languageContent.getLanguage().getId())) {
664 	        			languageSpecificContent.add(languageContent);
665 	        		}
666 	        	}        	
667 	        }
668         }
669         return languageSpecificContent;
670     }
671     
672 
673 
674     /***
675      * Gets the languages.
676      *
677      * @return the languages
678      */
679     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
680     @JoinTable(name = "AgencyLanguages")
681     @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
682     public Set<Language> getLanguages() {
683         return languages;
684     }
685 
686     /***
687      * Gets the name.
688      *
689      * @return the name
690      */
691     public String getName() {
692         return name;
693     }
694 
695     /***
696      * Gets the portal by id.
697      *
698      * @param portalId the portal id
699      * @return the portal by id
700      */
701     @Transient
702     public Portal getPortalById(final Long portalId) {
703         for (final Portal portal : portals) {
704             if (portalId.equals(portal.getId())) {
705                 return portal;
706             }
707         }
708         return null;
709     }
710 
711     /***
712      * Gets the portals.
713      *
714      * @return the portals
715      */
716     @OneToMany(mappedBy = "agency", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
717     @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
718     public Set<Portal> getPortals() {
719         return portals;
720     }
721 
722     /* (non-Javadoc)
723      * @see com.hack23.cia.model.core.BaseEntity#getVersion()
724      */
725     @Override
726     @Version
727     public Long getVersion() {
728         return version;
729     }
730 
731     /***
732      * Sets the id.
733      *
734      * @param id the new id
735      */
736     public void setId(final Long id) {
737         this.id = id;
738     }
739 
740     /***
741      * Sets the language content.
742      *
743      * @param languageContent the new language content
744      */
745     public void setLanguageContent(final Set<LanguageContent> languageContent) {
746         this.languageContent = languageContent;
747     }
748     
749     /***
750      * Sets the languages.
751      *
752      * @param languages the new languages
753      */
754     public void setLanguages(final Set<Language> languages) {
755         this.languages = languages;
756     }
757 
758     /***
759      * Sets the name.
760      *
761      * @param name the new name
762      */
763     public void setName(final String name) {
764         this.name = name;
765     }
766 
767     /***
768      * Sets the portals.
769      *
770      * @param portals the new portals
771      */
772     public void setPortals(final Set<Portal> portals) {
773         this.portals = portals;
774     }
775 
776     /***
777      * Sets the version.
778      *
779      * @param version the new version
780      */
781     public void setVersion(final Long version) {
782         this.version = version;
783     }
784 
785 }