1 24 package org.riotfamily.riot.workflow.status.support; 25 26 import java.util.Collection ; 27 import java.util.Collections ; 28 import java.util.Locale ; 29 30 import org.riotfamily.common.util.FormatUtils; 31 import org.riotfamily.riot.workflow.status.StatusMessage; 32 import org.riotfamily.riot.workflow.status.StatusMonitor; 33 import org.springframework.beans.factory.BeanNameAware; 34 import org.springframework.context.MessageSource; 35 import org.springframework.context.MessageSourceAware; 36 37 42 public abstract class AbstractStatusMonitor implements StatusMonitor, 43 MessageSourceAware, BeanNameAware { 44 45 private MessageSource messageSource; 46 47 private String messageKey; 48 49 private String link; 50 51 private long cacheMillis; 52 53 private long lastUpdate; 54 55 private Object [] args; 56 57 private boolean hideZeroStatus; 58 59 public void setMessageSource(MessageSource messageSource) { 60 this.messageSource = messageSource; 61 } 62 63 public void setMessageKey(String messageKey) { 64 this.messageKey = messageKey; 65 } 66 67 public boolean isHideZeroStatus() { 68 return this.hideZeroStatus; 69 } 70 71 public void setHideZeroStatus(boolean hideZeroStatus) { 72 this.hideZeroStatus = hideZeroStatus; 73 } 74 75 public void setBeanName(String name) { 76 if (messageKey == null) { 77 messageKey = name; 78 } 79 } 80 81 public void setLink(String link) { 82 this.link = link; 83 } 84 85 public void setCache(String period) { 86 cacheMillis = FormatUtils.parseMillis(period); 87 } 88 89 public Collection getMessages(Locale locale) { 90 updateArgs(); 91 if (isVisible(args)) { 92 String message = messageSource.getMessage(messageKey, args, locale); 93 return Collections.singleton(new StatusMessage(message, link)); 94 } 95 return null; 96 } 97 98 private synchronized void updateArgs() { 99 if (lastUpdate + cacheMillis < System.currentTimeMillis()) { 100 this.args = FormatUtils.htmlEscapeArgs(getArgs()); 101 } 102 } 103 104 protected boolean isVisible(Object [] args) { 105 if (args == null || args.length == 0) { 106 return false; 107 } 108 if (hideZeroStatus && args[0] instanceof Number ) { 109 return ((Number ) args[0]).intValue() > 0; 110 } 111 return true; 112 } 113 114 121 protected abstract Object [] getArgs(); 122 123 } 124 | Popular Tags |