1 16 package org.apache.jetspeed.modules.actions.portlets.designer; 17 18 import java.io.File ; 19 import java.io.FileInputStream ; 20 import java.io.FileOutputStream ; 21 import java.util.Hashtable ; 22 import java.util.Properties ; 23 24 import org.apache.jetspeed.modules.actions.portlets.VelocityPortletAction; 25 26 import org.apache.jetspeed.portal.portlets.VelocityPortlet; 27 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 28 import org.apache.jetspeed.services.logging.JetspeedLogger; 29 import org.apache.turbine.services.resources.TurbineResources; 30 import org.apache.turbine.util.RunData; 31 import org.apache.turbine.util.upload.FileItem; 32 import org.apache.velocity.context.Context; 33 34 40 public class HeaderAction extends VelocityPortletAction 41 { 42 private static final JetspeedLogger log = JetspeedLogFactoryService.getLogger(HeaderAction.class.getName()); 43 private static final String IMAGES_DIRECTORY = "images/designer/"; 44 private static final String DEFAULT_CSS = "css/default.css"; 45 46 private String fileTypes[] = 47 { "image/jpg", 48 "image/gif", 49 "image/jpeg", 50 "application/x-shockwave-flash", 51 "image/png", 52 "image/x-png" 53 }; 54 55 56 protected void buildNormalContext(VelocityPortlet portlet, Context context, 57 RunData rundata) throws Exception 58 { 59 60 } 61 62 public void doUpload(RunData rundata, Context context) 63 { 64 Hashtable ht = new Hashtable (); 65 66 boolean setLogo = rundata.getParameters().getBoolean("setLogo"); 67 boolean setBgImage = rundata.getParameters().getBoolean("setBgImage"); 68 boolean setBgColor = rundata.getParameters().getBoolean("setBgColor"); 69 boolean setFontFace = rundata.getParameters().getBoolean("setFontFace"); 70 boolean setFontSize = rundata.getParameters().getBoolean("setFontSize"); 71 boolean setFontColor = rundata.getParameters().getBoolean( 72 "setFontColor"); 73 boolean setTitle = rundata.getParameters().getBoolean("setTitle"); 74 75 FileItem fileLogo = null; 76 FileItem fileBgImage = null; 77 String bgColor = ""; 78 String fontFace = ""; 79 String fontSize = ""; 80 String fontColor = ""; 81 String bgImage = ""; 82 String title = ""; 83 84 if (setLogo) 85 { 86 fileLogo = rundata.getParameters().getFileItem("imgLogo"); 87 } 88 89 if (setBgImage) 90 { 91 String sampleBg = rundata.getParameters() 92 .getString("sampleBgImage").trim(); 93 if (sampleBg != null && sampleBg.length() != 0) 94 { 95 bgImage = IMAGES_DIRECTORY + sampleBg; 96 } else 97 { 98 fileBgImage = rundata.getParameters().getFileItem("bgImage"); 99 } 100 } 101 102 if (setBgColor) 103 { 104 bgColor = rundata.getParameters().getString("bgColor").trim(); 105 } 106 if (setFontFace) 107 { 108 fontFace = rundata.getParameters().getString("fontFace").trim(); 109 } 110 if (setFontSize) 111 { 112 fontSize = rundata.getParameters().getString("fontSize").trim(); 113 } 114 if (setFontColor) 115 { 116 fontColor = rundata.getParameters().getString("fontColor").trim(); 117 } 118 if (setTitle) 119 { 120 title = rundata.getParameters().getString("portalTitle").trim(); 121 } 122 123 log.info("fontColor : " + fontColor); 124 125 String message = ""; 126 try 127 { 128 String logo = ""; 129 130 String slash = System.getProperty("file.separator"); 131 String imagePath = FormatPath.normalizeDirectoryPath(rundata 132 .getServletContext().getRealPath("/" + IMAGES_DIRECTORY)); 133 134 if (fileLogo != null) 135 { 136 File file = new File (fileLogo.getFileName()); 137 logo = file.getName(); 138 139 int index = logo.lastIndexOf("\\"); 140 int index2 = logo.lastIndexOf("//"); 141 142 if (index > 0) 143 { 144 logo = logo.substring(index + 1); 145 } 146 147 if (index2 > 0) 148 { 149 logo = logo.substring(index2 + 1); 150 } 151 152 File f = new File (imagePath + logo); 153 if (f.exists()) 154 { 155 f.delete(); } 157 FileUploader fu = new FileUploader(); 158 boolean hasUploaded = fu.upload(fileLogo, imagePath, fileTypes); 159 162 if (hasUploaded == true) 163 { 164 logo = IMAGES_DIRECTORY + logo; 165 } else 166 { 167 context 168 .put( 169 "logoStatus", 170 "Error occurred while uploading " 171 + logo 172 + ". Only gif, jpg, and jpeg image files can be uploaded. "); 173 logo = ""; 174 } 175 176 178 } 179 180 if (fileBgImage != null) 181 { 182 File file1 = new File (fileBgImage.getFileName()); 183 bgImage = file1.getName(); 184 185 int index = bgImage.lastIndexOf("\\"); 186 int index2 = bgImage.lastIndexOf("//"); 187 188 if (index > 0) 189 { 190 bgImage = bgImage.substring(index + 1); 191 } 192 193 if (index2 > 0) 194 { 195 bgImage = bgImage.substring(index2 + 1); 196 } 197 198 File f = new File (imagePath + bgImage); 199 if (f.exists()) f.delete(); FileUploader fu = new FileUploader(); 201 boolean hasUploaded = fu.upload(fileBgImage, imagePath, 202 fileTypes); 203 log.info("hasUploaded : " + hasUploaded); 204 205 if (hasUploaded == true) 206 bgImage = IMAGES_DIRECTORY + bgImage; 207 else 208 { 209 context 210 .put( 211 "bgStatus", 212 "Error while uploading " 213 + bgImage 214 + ". Only gif, jpg, and jpeg image files can be uploaded. "); 215 bgImage = ""; 216 } 217 218 } 219 220 boolean hasColor = (bgColor == null || bgColor.length() < 1); 221 boolean hasFontFace = (fontFace == null || fontFace.length() < 1); 222 boolean hasFontSize = (fontSize == null || fontSize.length() < 1); 223 boolean hasFontColor = (fontColor == null || fontColor.length() < 1); 224 225 if (hasColor) 226 { 227 bgColor = "white"; 228 } 229 if (hasFontFace) 230 { 231 fontFace = "verdana"; 232 } 233 if (hasFontSize) 234 { 235 fontSize = "12"; 236 } 237 if (hasFontColor) 238 { 239 fontColor = "black"; 240 } 241 242 ht.put("logo", logo); 243 ht.put("BgImage", bgImage); 244 ht.put("BgColor", bgColor); 245 ht.put("FontFace", fontFace); 246 ht.put("FontSize", fontSize); 247 ht.put("FontColor", fontColor); 248 ht.put("PortalTitle", title); 249 ht.put("SetLogo", String.valueOf(setLogo)); 250 ht.put("SetBgImage", String.valueOf(setBgImage)); 251 ht.put("SetBgColor", String.valueOf(setBgColor)); 252 ht.put("SetFontFace", String.valueOf(setFontFace)); 253 ht.put("SetFontSize", String.valueOf(setFontSize)); 254 ht.put("SetFontColor", String.valueOf(setFontColor)); 255 ht.put("SetTitle", String.valueOf(setTitle)); 256 257 editHeader(rundata, context, ht); 258 259 } catch (Exception ee) 260 { 261 message = ee.getMessage(); 262 } 263 } 264 265 public void editHeader(RunData rundata, Context context, Hashtable ht) 266 throws Exception 267 { 268 try 269 { 270 String logo = (String ) ht.get("logo"); 271 String bgImage = (String ) ht.get("BgImage"); 272 String bgColor = (String ) ht.get("BgColor"); 273 String fontSize = (String ) ht.get("FontSize"); 274 String fontColor = (String ) ht.get("FontColor"); 275 String title = (String ) ht.get("PortalTitle"); 276 277 boolean setLogo = Boolean.valueOf((String ) ht.get("SetLogo")).booleanValue(); 278 boolean setBgImage = Boolean.valueOf((String ) ht.get("SetBgImage")) 279 .booleanValue(); 280 boolean setBgColor = Boolean.valueOf((String ) ht.get("SetBgColor")) 281 .booleanValue(); 282 boolean setFontSize = Boolean.valueOf((String ) ht.get("SetFontSize")) 283 .booleanValue(); 284 boolean setFontColor = Boolean.valueOf((String ) ht.get("SetFontColor")) 285 .booleanValue(); 286 boolean setPortalTitle = Boolean.valueOf((String ) ht.get("SetTitle")) 287 .booleanValue(); 288 289 String DEFAULT_ROOT = File.separator + "WEB-INF" + File.separator 290 + "conf" + File.separator; 291 String root = rundata.getServletConfig().getServletContext() 292 .getRealPath(DEFAULT_ROOT) 293 + File.separator; 294 295 Properties prop = new Properties (); 296 prop.clear(); 297 prop.load(new FileInputStream (root + "JetspeedResources.properties")); 298 299 TurbineResources trProp = (TurbineResources) TurbineResources 300 .getResources(root + "JetspeedResources.properties"); 301 302 int counter = 0; 303 if (setBgImage && !setBgColor && (counter == 0)) 304 { 305 setBgColor = true; 306 bgColor = "FFFFFF"; 307 counter = 1; 308 } 309 if (!setBgImage && setBgColor && (counter == 0)) 310 { 311 setBgImage = true; 312 bgImage = ""; 313 counter = 1; 314 } 315 if (setBgImage && setBgColor && (counter == 0)) 316 { 317 setBgColor = true; 318 bgColor = "FFFFFF"; 319 counter = 1; 320 } 321 322 if (setLogo) 323 { 324 TurbineResources.setProperty("topnav.logo.file", logo); 325 prop.setProperty("topnav.logo.file", logo); 326 } 327 if (setBgImage) 328 { 329 TurbineResources.setProperty("topnav.bg.image", bgImage); 330 prop.setProperty("topnav.bg.image", bgImage); 331 } 332 if (setBgColor) 333 { 334 TurbineResources.setProperty("topnav.bg.color", bgColor); 335 prop.setProperty("topnav.bg.color", bgColor); 336 } 337 if (setFontSize) 338 { 339 TurbineResources.setProperty("topnav.font.size", fontSize); 340 prop.setProperty("topnav.font.size", fontSize); 341 } 342 if (setFontColor) 343 { 344 TurbineResources.setProperty("topnav.font.color", fontColor); 345 prop.setProperty("topnav.font.color", fontColor); 346 } 347 if (setPortalTitle) 348 { 349 TurbineResources.setProperty("portal.title", title); 350 prop.setProperty("portal.title", title); 351 } 352 353 FileOutputStream stream = new FileOutputStream (root + "JetspeedResources.properties"); 354 prop.store(stream, "topnav.logo.file"); 355 prop.store(stream, "topnav.bg.image"); 356 prop.store(stream, "topnav.bg.color"); 357 prop.store(stream, "topnav.font.size"); 358 prop.store(stream, "topnav.font.color"); 359 stream.close(); 360 361 String logoJR = prop.getProperty("topnav.logo.file"); 362 String bgImageJR = prop.getProperty("topnav.bg.image"); 363 String bgColorJR = prop.getProperty("topnav.bg.color"); 364 String fontSizeJR = prop.getProperty("topnav.font.size"); 365 String fontColorJR = prop.getProperty("topnav.font.color"); 366 367 log.info("new Logo : " + logoJR); 368 log.info("new bgImageJR : " + bgImageJR); 369 log.info("new bgColorJR : " + bgColorJR); 370 log.info("new fontSizeJR : " + fontSizeJR); 371 log.info("new fontColorJR : " + fontColorJR); 372 373 prop = null; 374 System.gc(); 375 376 context.put("settingStatus", "Successfully changed settings."); 377 } catch (Exception e) 378 { 379 context.put("settingStatus", 380 "Error occurred while changing settings."); 381 log.error(e); 382 } 383 } 384 385 public void doDefault(RunData rundata, Context context) throws Exception 386 { 387 try 388 { 389 String logo = IMAGES_DIRECTORY + "jetspeed-logo.gif"; 390 String bgImage = ""; 391 String fontSize = "10"; 392 String title = "Jakarta Jetspeed"; 393 394 String DEFAULT_ROOT = File.separator + "WEB-INF" + File.separator 395 + "conf" + File.separator; 396 String root = rundata.getServletConfig().getServletContext() 397 .getRealPath(DEFAULT_ROOT) 398 + File.separator; 399 400 Properties prop = new Properties (); 401 prop 402 .load(new FileInputStream (root 403 + "JetspeedResources.properties")); 404 prop.clone(); 405 406 TurbineResources trProp = (TurbineResources) TurbineResources 407 .getResources(root + "JetspeedResources.properties"); 408 409 TurbineResources.setProperty("portal.title", title); 410 prop.setProperty("portal.title", title); 411 TurbineResources.setProperty("topnav.logo.file", logo); 412 prop.setProperty("topnav.logo.file", logo); 413 TurbineResources.setProperty("topnav.bg.image",""); 414 prop.setProperty("topnav.bg.image",""); 415 TurbineResources.setProperty("topnav.bg.color", ""); 416 prop.setProperty("topnav.bg.color", ""); 417 418 TurbineResources.setProperty("topnav.font.size", ""); 419 prop.setProperty("topnav.font.size", ""); 420 421 TurbineResources.setProperty("topnav.font.color", ""); 422 prop.setProperty("topnav.font.color", ""); 423 424 425 FileOutputStream stream = new FileOutputStream (root + "JetspeedResources.properties"); 426 427 prop.save(stream, "topnav.logo.file"); 428 429 prop.save(stream, "portal.title"); 430 prop.save(stream, "topnav.bg.image"); 431 prop.save(stream, "topnav.bg.color"); 432 prop.save(stream, "ptopnav.font.size"); 433 prop.save(stream, "ptopnav.font.size"); 434 stream.close(); 435 436 context.put("settingStatus", 437 "Successfully changed to default settings."); 438 } catch (Exception e) 439 { 440 context.put("settingStatus", 441 "Error occurred while changing to default settings. "); 442 log.error(e); 443 } 444 } 445 446 447 869 870 } 871 872 | Popular Tags |