1 16 package dlog4j.tags; 17 18 import java.text.SimpleDateFormat ; 19 import java.util.Calendar ; 20 import java.util.Locale ; 21 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.jsp.JspException ; 24 25 import net.sf.hibernate.Session; 26 27 import dlog4j.LogManager; 28 import dlog4j.SiteManager; 29 import dlog4j.formbean.UserForm; 30 31 35 public class CalendarTag extends DlogBaseTag { 36 37 protected String paramYear = "log_year"; 38 protected String paramMonth = "log_month"; 39 40 protected String yearId = "year"; 41 protected String monthId = "month"; 42 protected String titleId = "title"; 43 protected String datasId = "datas"; 44 protected String logcId = "logcs"; 45 46 private final static SimpleDateFormat sdf = new SimpleDateFormat ("MMMMM yyyy",Locale.ENGLISH); 47 50 public int doStartTag() throws JspException { 51 Calendar cal = Calendar.getInstance(); 52 try{ 53 int year = Integer.parseInt(pageContext.getRequest().getParameter(paramYear)); 54 if(year>0) 55 cal.set(Calendar.YEAR,year); 56 }catch(Exception e){} 57 try{ 58 int month = Integer.parseInt(pageContext.getRequest().getParameter(paramMonth)); 59 if(month>0&&month<13) 60 cal.set(Calendar.MONTH,month-1); 61 else if(month==0){ 62 cal.set(Calendar.MONTH,11); 63 cal.add(Calendar.YEAR,-1); 64 } 65 else if(month==13){ 66 cal.set(Calendar.MONTH,0); 67 cal.add(Calendar.YEAR,1); 68 } 69 }catch(Exception e){} 70 71 int[][] datas = buildCalendar(cal); 72 pageContext.setAttribute(datasId,datas); 73 74 pageContext.setAttribute(yearId,new Integer (cal.get(Calendar.YEAR))); 75 pageContext.setAttribute(monthId,new Integer (cal.get(Calendar.MONTH)+1)); 76 77 pageContext.setAttribute(titleId,sdf.format(cal.getTime())); 78 79 Session ssn = null; 81 try{ 82 ssn = getSession(); 83 UserForm u = UserForm.getLoginUser((HttpServletRequest )pageContext.getRequest()); 84 int[] lcs = LogManager.statLogs(ssn,SiteManager.getCurrentSite(pageContext.getRequest()),u,cal); 85 pageContext.setAttribute(logcId, lcs); 86 }catch(Exception e){ 87 throw new JspException (e); 88 }finally{ 89 if(ssn!=null) 90 try{ 91 closeSession(ssn); 92 }catch(Exception e){} 93 } 94 95 return SKIP_BODY; 96 } 97 103 protected static int[][] buildCalendar(Calendar cal){ 104 cal.set(Calendar.DAY_OF_MONTH,1); 105 int firstDateInWeek = cal.get(Calendar.DAY_OF_WEEK)-1; 106 int dateOfMonth = getMonthDateCount(cal); 107 int base = dateOfMonth + firstDateInWeek; 108 int row = base / 7; 109 row += ((base%7)>0)?1:0; 110 int[][] cals = new int[row][7]; 111 int iCol=firstDateInWeek,iRow=0; 112 for(int i=1;i<=dateOfMonth;i++){ 113 cals[iRow][iCol] = i; 114 if(iCol==6){ 115 iCol = 0; 116 iRow++; 117 } 118 else 119 iCol++; 120 } 121 return cals; 122 } 123 128 protected static int getMonthDateCount(Calendar cal){ 129 Calendar cal2 = (Calendar )cal.clone(); 130 cal2.add(Calendar.MONTH,1); 131 cal2.set(Calendar.DAY_OF_MONTH,1); 132 cal2.add(Calendar.DAY_OF_MONTH,-1); 133 return cal2.get(Calendar.DAY_OF_MONTH); 134 } 135 136 public static void main(String [] args) throws Exception { 137 int[][] days = buildCalendar(Calendar.getInstance()); 138 for(int i=0;i<days.length;i++){ 139 for(int j=0;j<days[i].length;j++) 140 System.out.print(days[i][j]+" "); 141 System.out.println(); 142 } 143 } 144 145 148 public String getDatasId() { 149 return datasId; 150 } 151 152 155 public String getMonthId() { 156 return monthId; 157 } 158 159 162 public String getTitleId() { 163 return titleId; 164 } 165 166 169 public String getYearId() { 170 return yearId; 171 } 172 173 176 public void setDatasId(String string) { 177 datasId = string; 178 } 179 180 183 public void setMonthId(String string) { 184 monthId = string; 185 } 186 187 190 public void setTitleId(String string) { 191 titleId = string; 192 } 193 194 197 public void setYearId(String string) { 198 yearId = string; 199 } 200 201 204 public String getParamMonth() { 205 return paramMonth; 206 } 207 208 211 public String getParamYear() { 212 return paramYear; 213 } 214 215 218 public void setParamMonth(String string) { 219 paramMonth = string; 220 } 221 222 225 public void setParamYear(String string) { 226 paramYear = string; 227 } 228 229 232 public String getLogcId() { 233 return logcId; 234 } 235 236 239 public void setLogcId(String string) { 240 logcId = string; 241 } 242 243 } 244 | Popular Tags |