Mercurial > hg > LGDataverses
comparison src/main/java/edu/harvard/iq/dataverse/ThemeWidgetFragment.java @ 10:a50cf11e5178
Rewrite LGDataverse completely upgrading to dataverse4.0
| author | Zoe Hong <zhong@mpiwg-berlin.mpg.de> |
|---|---|
| date | Tue, 08 Sep 2015 17:00:21 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 9:5926d6419569 | 10:a50cf11e5178 |
|---|---|
| 1 /* | |
| 2 * To change this license header, choose License Headers in Project Properties. | |
| 3 * To change this template file, choose Tools | Templates | |
| 4 * and open the template in the editor. | |
| 5 */ | |
| 6 package edu.harvard.iq.dataverse; | |
| 7 | |
| 8 import edu.harvard.iq.dataverse.api.Datasets; | |
| 9 import edu.harvard.iq.dataverse.engine.command.Command; | |
| 10 import edu.harvard.iq.dataverse.engine.command.exception.CommandException; | |
| 11 import edu.harvard.iq.dataverse.engine.command.impl.UpdateDataverseThemeCommand; | |
| 12 import edu.harvard.iq.dataverse.util.JsfHelper; | |
| 13 import static edu.harvard.iq.dataverse.util.JsfHelper.JH; | |
| 14 import java.io.File; | |
| 15 import java.io.IOException; | |
| 16 import java.net.MalformedURLException; | |
| 17 import java.net.URL; | |
| 18 import java.nio.file.Files; | |
| 19 import java.nio.file.Paths; | |
| 20 import java.nio.file.StandardCopyOption; | |
| 21 import java.util.logging.Level; | |
| 22 import java.util.logging.Logger; | |
| 23 import javax.annotation.PreDestroy; | |
| 24 import javax.ejb.EJB; | |
| 25 import javax.faces.application.FacesMessage; | |
| 26 import javax.faces.component.UIComponent; | |
| 27 import javax.faces.component.html.HtmlInputText; | |
| 28 import javax.faces.context.FacesContext; | |
| 29 import javax.faces.validator.ValidatorException; | |
| 30 import javax.faces.view.ViewScoped; | |
| 31 import javax.inject.Inject; | |
| 32 import javax.inject.Named; | |
| 33 | |
| 34 import org.apache.commons.lang.StringUtils; | |
| 35 import org.primefaces.context.RequestContext; | |
| 36 | |
| 37 import org.primefaces.event.FileUploadEvent; | |
| 38 import org.primefaces.model.UploadedFile; | |
| 39 | |
| 40 /** | |
| 41 * | |
| 42 * @author ellenk | |
| 43 */ | |
| 44 @ViewScoped | |
| 45 @Named | |
| 46 public class ThemeWidgetFragment implements java.io.Serializable { | |
| 47 static final String DEFAULT_LOGO_BACKGROUND_COLOR = "F5F5F5"; | |
| 48 static final String DEFAULT_BACKGROUND_COLOR = "F5F5F5"; | |
| 49 static final String DEFAULT_LINK_COLOR = "428BCA"; | |
| 50 static final String DEFAULT_TEXT_COLOR = "888888"; | |
| 51 private static final Logger logger = Logger.getLogger(ThemeWidgetFragment.class.getCanonicalName()); | |
| 52 | |
| 53 @Inject DataversePage dataversePage; | |
| 54 private File tempDir; | |
| 55 private File uploadedFile; | |
| 56 private Dataverse editDv= new Dataverse(); | |
| 57 private HtmlInputText linkUrlInput; | |
| 58 private HtmlInputText taglineInput; | |
| 59 | |
| 60 @Inject | |
| 61 DataverseSession session; | |
| 62 @EJB | |
| 63 EjbDataverseEngine commandEngine; | |
| 64 @EJB | |
| 65 DataverseServiceBean dataverseServiceBean; | |
| 66 | |
| 67 public HtmlInputText getLinkUrlInput() { | |
| 68 return linkUrlInput; | |
| 69 } | |
| 70 | |
| 71 public void setLinkUrlInput(HtmlInputText linkUrlInput) { | |
| 72 this.linkUrlInput = linkUrlInput; | |
| 73 } | |
| 74 | |
| 75 public HtmlInputText getTaglineInput() { | |
| 76 return taglineInput; | |
| 77 } | |
| 78 | |
| 79 public void setTaglineInput(HtmlInputText taglineInput) { | |
| 80 this.taglineInput = taglineInput; | |
| 81 } | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 private void createTempDir() { | |
| 87 try { | |
| 88 File tempRoot = Files.createDirectories(Paths.get("../docroot/logos/temp")).toFile(); | |
| 89 tempDir = Files.createTempDirectory(tempRoot.toPath(),editDv.getId().toString()).toFile(); | |
| 90 } catch (IOException e) { | |
| 91 throw new RuntimeException("Error creating temp directory", e); // improve error handling | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 @PreDestroy | |
| 96 /** | |
| 97 * Cleanup by deleting temp directory and uploaded files | |
| 98 */ | |
| 99 public void cleanupTempDirectory() { | |
| 100 try { | |
| 101 | |
| 102 if (tempDir != null) { | |
| 103 for (File f : tempDir.listFiles()) { | |
| 104 Files.deleteIfExists(f.toPath()); | |
| 105 } | |
| 106 Files.deleteIfExists(tempDir.toPath()); | |
| 107 } | |
| 108 } catch (IOException e) { | |
| 109 throw new RuntimeException("Error deleting temp directory", e); // improve error handling | |
| 110 } | |
| 111 uploadedFile=null; | |
| 112 tempDir=null; | |
| 113 } | |
| 114 | |
| 115 public void checkboxListener() { | |
| 116 // not sure if this is needed for the ajax component | |
| 117 } | |
| 118 | |
| 119 | |
| 120 public void initEditDv() { | |
| 121 editDv = dataverseServiceBean.find(editDv.getId()); | |
| 122 if (editDv.getOwner()==null) { | |
| 123 editDv.setThemeRoot(true); | |
| 124 } | |
| 125 if (editDv.getDataverseTheme()==null && editDv.isThemeRoot()) { | |
| 126 editDv.setDataverseTheme(initDataverseTheme()); | |
| 127 | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 private DataverseTheme initDataverseTheme() { | |
| 132 DataverseTheme dvt = new DataverseTheme(); | |
| 133 dvt.setLinkColor(DEFAULT_LINK_COLOR); | |
| 134 dvt.setLogoBackgroundColor(DEFAULT_LOGO_BACKGROUND_COLOR); | |
| 135 dvt.setBackgroundColor(DEFAULT_BACKGROUND_COLOR); | |
| 136 dvt.setTextColor(DEFAULT_TEXT_COLOR); | |
| 137 dvt.setDataverse(editDv); | |
| 138 return dvt; | |
| 139 } | |
| 140 | |
| 141 public Dataverse getEditDv() { | |
| 142 return editDv; | |
| 143 } | |
| 144 | |
| 145 public void setEditDv(Dataverse editDV) { | |
| 146 this.editDv = editDV; | |
| 147 | |
| 148 | |
| 149 } | |
| 150 public void validateTagline(FacesContext context, UIComponent component, Object value) throws ValidatorException { | |
| 151 | |
| 152 if (!StringUtils.isEmpty((String) value) && ((String) value).length() > 140) { | |
| 153 FacesMessage msg = new FacesMessage("Tagline must be at most 140 characters."); | |
| 154 msg.setSeverity(FacesMessage.SEVERITY_ERROR); | |
| 155 | |
| 156 throw new ValidatorException(msg); | |
| 157 } | |
| 158 | |
| 159 } | |
| 160 | |
| 161 public void validateUrl(FacesContext context, UIComponent component, Object value) throws ValidatorException { | |
| 162 try { | |
| 163 if (!StringUtils.isEmpty((String)value)){ | |
| 164 URL test = new URL((String)value); | |
| 165 } | |
| 166 } catch(MalformedURLException e) { | |
| 167 FacesMessage msg = | |
| 168 new FacesMessage(" URL validation failed.", | |
| 169 "Please provide URL."); | |
| 170 msg.setSeverity(FacesMessage.SEVERITY_ERROR); | |
| 171 | |
| 172 throw new ValidatorException(msg); | |
| 173 } | |
| 174 | |
| 175 } | |
| 176 | |
| 177 | |
| 178 public String getTempDirName() { | |
| 179 if (tempDir!=null) { | |
| 180 return tempDir.getName(); | |
| 181 } else { | |
| 182 return null; | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 public boolean uploadExists() { | |
| 187 return uploadedFile!=null; | |
| 188 } | |
| 189 /** | |
| 190 * Copy uploaded file to temp area, until we are ready to save | |
| 191 * Copy filename into Dataverse logo | |
| 192 * @param event | |
| 193 */ | |
| 194 | |
| 195 public void handleImageFileUpload(FileUploadEvent event) { | |
| 196 | |
| 197 logger.finer("entering fileUpload"); | |
| 198 if (this.tempDir==null) { | |
| 199 createTempDir(); | |
| 200 logger.finer("created tempDir"); | |
| 201 } | |
| 202 UploadedFile uFile = event.getFile(); | |
| 203 try { | |
| 204 uploadedFile = new File(tempDir, uFile.getFileName()); | |
| 205 if (!uploadedFile.exists()) { | |
| 206 uploadedFile.createNewFile(); | |
| 207 } | |
| 208 logger.finer("created file"); | |
| 209 Files.copy(uFile.getInputstream(), uploadedFile.toPath(),StandardCopyOption.REPLACE_EXISTING); | |
| 210 logger.finer("copied inputstream to file"); | |
| 211 editDv.getDataverseTheme().setLogo(uFile.getFileName()); | |
| 212 | |
| 213 } catch (IOException e) { | |
| 214 logger.finer("caught IOException"); | |
| 215 logger.throwing("ThemeWidgetFragment", "handleImageFileUpload", e); | |
| 216 throw new RuntimeException("Error uploading logo file", e); // improve error handling | |
| 217 } | |
| 218 // If needed, set the default values for the logo | |
| 219 if (editDv.getDataverseTheme().getLogoFormat()==null) { | |
| 220 editDv.getDataverseTheme().setLogoFormat(DataverseTheme.ImageFormat.SQUARE); | |
| 221 } | |
| 222 logger.finer("end handelImageFileUpload"); | |
| 223 } | |
| 224 | |
| 225 public void removeLogo() { | |
| 226 editDv.getDataverseTheme().setLogo(null); | |
| 227 this.cleanupTempDirectory(); | |
| 228 | |
| 229 } | |
| 230 | |
| 231 public boolean getInheritCustomization() { | |
| 232 boolean inherit= editDv==null ? true : !editDv.getThemeRoot(); | |
| 233 return inherit; | |
| 234 } | |
| 235 | |
| 236 public void setInheritCustomization(boolean inherit) { | |
| 237 editDv.setThemeRoot(!inherit); | |
| 238 if (!inherit) { | |
| 239 if (editDv.getDataverseTheme(true)==null) { | |
| 240 editDv.setDataverseTheme(initDataverseTheme()); | |
| 241 } | |
| 242 } | |
| 243 } | |
| 244 public void resetForm() { | |
| 245 RequestContext context = RequestContext.getCurrentInstance(); | |
| 246 context.reset(":dataverseForm:themeWidgetsTabView"); | |
| 247 } | |
| 248 | |
| 249 public String cancel() { | |
| 250 return "dataverse?faces-redirect=true&alias="+editDv.getAlias(); // go to dataverse page | |
| 251 } | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 public String save() { | |
| 257 // If this Dv isn't the root, delete the uploaded file and remove theme | |
| 258 // before saving. | |
| 259 if (!editDv.isThemeRoot()) { | |
| 260 uploadedFile=null; | |
| 261 editDv.setDataverseTheme(null); | |
| 262 } | |
| 263 Command<Dataverse> cmd = new UpdateDataverseThemeCommand(editDv, this.uploadedFile, session.getUser()); | |
| 264 try { | |
| 265 dataversePage.setDataverse(commandEngine.submit(cmd)); | |
| 266 dataversePage.setEditMode(null); | |
| 267 | |
| 268 } catch (Exception ex) { | |
| 269 logger.log(Level.SEVERE, "error updating dataverse theme", ex); | |
| 270 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Dataverse Save Failed-", JH.localize("dataverse.theme.failure"))); | |
| 271 | |
| 272 return null; | |
| 273 } finally { | |
| 274 this.cleanupTempDirectory(); | |
| 275 } | |
| 276 JsfHelper.addSuccessMessage(JH.localize("dataverse.theme.success")); | |
| 277 return "dataverse?faces-redirect=true&alias="+editDv.getAlias(); // go to dataverse page | |
| 278 } | |
| 279 | |
| 280 | |
| 281 } | |
| 282 | |
| 283 | |
| 284 |
