Mercurial > hg > LGDataverses
comparison src/main/java/edu/harvard/iq/dataverse/TemplatePage.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 package edu.harvard.iq.dataverse; | |
| 2 | |
| 3 import edu.harvard.iq.dataverse.engine.command.Command; | |
| 4 import edu.harvard.iq.dataverse.engine.command.exception.CommandException; | |
| 5 import edu.harvard.iq.dataverse.engine.command.impl.UpdateDataverseCommand; | |
| 6 import edu.harvard.iq.dataverse.engine.command.impl.UpdateDataverseTemplateCommand; | |
| 7 import edu.harvard.iq.dataverse.util.JsfHelper; | |
| 8 import static edu.harvard.iq.dataverse.util.JsfHelper.JH; | |
| 9 import java.sql.Timestamp; | |
| 10 import java.util.Date; | |
| 11 import java.util.Set; | |
| 12 import javax.ejb.EJB; | |
| 13 import javax.ejb.EJBException; | |
| 14 import javax.faces.application.FacesMessage; | |
| 15 import javax.faces.context.FacesContext; | |
| 16 import javax.faces.view.ViewScoped; | |
| 17 import javax.inject.Inject; | |
| 18 import javax.inject.Named; | |
| 19 import javax.validation.ConstraintViolation; | |
| 20 import javax.validation.Validation; | |
| 21 import javax.validation.Validator; | |
| 22 import javax.validation.ValidatorFactory; | |
| 23 | |
| 24 /** | |
| 25 * | |
| 26 * @author skraffmiller | |
| 27 */ | |
| 28 @ViewScoped | |
| 29 @Named("TemplatePage") | |
| 30 public class TemplatePage implements java.io.Serializable { | |
| 31 | |
| 32 @EJB | |
| 33 TemplateServiceBean templateService; | |
| 34 | |
| 35 @EJB | |
| 36 DataverseServiceBean dataverseService; | |
| 37 | |
| 38 @EJB | |
| 39 EjbDataverseEngine commandEngine; | |
| 40 | |
| 41 @EJB | |
| 42 DataverseFieldTypeInputLevelServiceBean dataverseFieldTypeInputLevelService; | |
| 43 | |
| 44 @Inject | |
| 45 DataverseSession session; | |
| 46 | |
| 47 public enum EditMode { | |
| 48 | |
| 49 CREATE, METADATA | |
| 50 }; | |
| 51 | |
| 52 private Template template; | |
| 53 private Dataverse dataverse; | |
| 54 private EditMode editMode; | |
| 55 private Long ownerId; | |
| 56 private Long templateId; | |
| 57 | |
| 58 public Long getTemplateId() { | |
| 59 return templateId; | |
| 60 } | |
| 61 | |
| 62 public void setTemplateId(Long templateId) { | |
| 63 this.templateId = templateId; | |
| 64 } | |
| 65 | |
| 66 public Template getTemplate() { | |
| 67 return template; | |
| 68 } | |
| 69 | |
| 70 public void setTemplate(Template template) { | |
| 71 this.template = template; | |
| 72 } | |
| 73 | |
| 74 public Dataverse getDataverse() { | |
| 75 return dataverse; | |
| 76 } | |
| 77 | |
| 78 public void setDataverse(Dataverse dataverse) { | |
| 79 this.dataverse = dataverse; | |
| 80 } | |
| 81 | |
| 82 public EditMode getEditMode() { | |
| 83 return editMode; | |
| 84 } | |
| 85 | |
| 86 public void setEditMode(EditMode editMode) { | |
| 87 this.editMode = editMode; | |
| 88 } | |
| 89 | |
| 90 public Long getOwnerId() { | |
| 91 return ownerId; | |
| 92 } | |
| 93 | |
| 94 public void setOwnerId(Long ownerId) { | |
| 95 this.ownerId = ownerId; | |
| 96 } | |
| 97 | |
| 98 public void init() { | |
| 99 if (templateId != null) { // edit or view existing for a template | |
| 100 dataverse = dataverseService.find(ownerId); | |
| 101 template = templateService.find(templateId); | |
| 102 template.setDataverse(dataverse); | |
| 103 template.setMetadataValueBlocks(); | |
| 104 updateDatasetFieldInputLevels(); | |
| 105 } else if (ownerId != null) { | |
| 106 // create mode for a new template | |
| 107 dataverse = dataverseService.find(ownerId); | |
| 108 editMode = TemplatePage.EditMode.CREATE; | |
| 109 template = new Template(this.dataverse); | |
| 110 updateDatasetFieldInputLevels(); | |
| 111 } else { | |
| 112 throw new RuntimeException("On Template page without id or ownerid."); // improve error handling | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 private void updateDatasetFieldInputLevels(){ | |
| 117 Long dvIdForInputLevel = ownerId; | |
| 118 if (!dataverseService.find(ownerId).isMetadataBlockRoot()){ | |
| 119 dvIdForInputLevel = dataverseService.find(ownerId).getMetadataRootId(); | |
| 120 } | |
| 121 | |
| 122 for (DatasetField dsf: template.getFlatDatasetFields()){ | |
| 123 DataverseFieldTypeInputLevel dsfIl = dataverseFieldTypeInputLevelService.findByDataverseIdDatasetFieldTypeId(dvIdForInputLevel, dsf.getDatasetFieldType().getId()); | |
| 124 if (dsfIl != null){ | |
| 125 dsf.setInclude(dsfIl.isInclude()); | |
| 126 } else { | |
| 127 dsf.setInclude(true); | |
| 128 } | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 public void edit(TemplatePage.EditMode editMode) { | |
| 133 this.editMode = editMode; | |
| 134 } | |
| 135 | |
| 136 public String save() { | |
| 137 | |
| 138 boolean dontSave = false; | |
| 139 ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); | |
| 140 Validator validator = factory.getValidator(); | |
| 141 for (DatasetField dsf : template.getFlatDatasetFields()) { | |
| 142 dsf.setValidationMessage(null); // clear out any existing validation message | |
| 143 Set<ConstraintViolation<DatasetField>> constraintViolations = validator.validate(dsf); | |
| 144 for (ConstraintViolation<DatasetField> constraintViolation : constraintViolations) { | |
| 145 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Validation Error", constraintViolation.getMessage())); | |
| 146 dsf.setValidationMessage(constraintViolation.getMessage()); | |
| 147 dontSave = true; | |
| 148 break; // currently only support one message, so we can break out of the loop after the first constraint violation | |
| 149 } | |
| 150 for (DatasetFieldValue dsfv : dsf.getDatasetFieldValues()) { | |
| 151 dsfv.setValidationMessage(null); // clear out any existing validation message | |
| 152 Set<ConstraintViolation<DatasetFieldValue>> constraintViolations2 = validator.validate(dsfv); | |
| 153 for (ConstraintViolation<DatasetFieldValue> constraintViolation : constraintViolations2) { | |
| 154 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Validation Error", constraintViolation.getMessage())); | |
| 155 dsfv.setValidationMessage(constraintViolation.getMessage()); | |
| 156 dontSave = true; | |
| 157 break; // currently only support one message, so we can break out of the loop after the first constraint violation | |
| 158 } | |
| 159 } | |
| 160 } | |
| 161 if (dontSave) { | |
| 162 return ""; | |
| 163 } | |
| 164 boolean create = false; | |
| 165 Command cmd; | |
| 166 try { | |
| 167 if (editMode == EditMode.CREATE) { | |
| 168 template.setCreateTime(new Timestamp(new Date().getTime())); | |
| 169 template.setUsageCount(new Long(0)); | |
| 170 dataverse.getTemplates().add(template); | |
| 171 cmd = new UpdateDataverseCommand(dataverse, null, null, session.getUser(), null); | |
| 172 create = true; | |
| 173 commandEngine.submit(cmd); | |
| 174 } else { | |
| 175 cmd = new UpdateDataverseTemplateCommand(dataverse, template, session.getUser()); | |
| 176 commandEngine.submit(cmd); | |
| 177 } | |
| 178 | |
| 179 } catch (EJBException ex) { | |
| 180 StringBuilder error = new StringBuilder(); | |
| 181 error.append(ex + " "); | |
| 182 error.append(ex.getMessage() + " "); | |
| 183 Throwable cause = ex; | |
| 184 while (cause.getCause() != null) { | |
| 185 cause = cause.getCause(); | |
| 186 error.append(cause + " "); | |
| 187 error.append(cause.getMessage() + " "); | |
| 188 } | |
| 189 // | |
| 190 //FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Template Save Failed", " - " + error.toString())); | |
| 191 System.out.print("dataverse " + dataverse.getName()); | |
| 192 System.out.print("Ejb exception"); | |
| 193 System.out.print(error.toString()); | |
| 194 JH.addMessage(FacesMessage.SEVERITY_FATAL, "Template Save Failed"); | |
| 195 return null; | |
| 196 } catch (CommandException ex) { | |
| 197 System.out.print("command exception"); | |
| 198 System.out.print(ex.toString()); | |
| 199 //FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Template Save Failed", " - " + ex.toString())); | |
| 200 JH.addMessage(FacesMessage.SEVERITY_FATAL, "Template Save Failed"); | |
| 201 return null; | |
| 202 //logger.severe(ex.getMessage()); | |
| 203 } | |
| 204 editMode = null; | |
| 205 String msg = (create)? "Template has been created.": "Template has been edited and saved."; | |
| 206 JsfHelper.addFlashMessage(msg); | |
| 207 return "/manage-templates.xhtml?dataverseId=" + dataverse.getId() + "&faces-redirect=true"; | |
| 208 } | |
| 209 | |
| 210 public void cancel() { | |
| 211 editMode = null; | |
| 212 } | |
| 213 | |
| 214 } |
