1 19 20 package org.netbeans.modules.j2ee.persistence.unit; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import org.netbeans.api.project.FileOwnerQuery; 25 import org.netbeans.api.project.Project; 26 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit; 27 import org.netbeans.modules.j2ee.persistence.wizard.Util; 28 import org.netbeans.modules.xml.multiview.Error; 29 30 35 public class PersistenceValidator { 36 37 private final PUDataObject puDataObject; 38 private List <Error > errors = new ArrayList <Error >(); 39 40 45 public PersistenceValidator(PUDataObject puDataObject) { 46 this.puDataObject = puDataObject; 47 } 48 49 53 public List <Error > validate(){ 54 validateName(); 55 validateExcludeUnlisted(); 56 validateJarFiles(); 57 return errors; 58 } 59 60 63 private void validateName(){ 64 PersistenceUnit[] persistenceUnits = puDataObject.getPersistence().getPersistenceUnit(); 65 for (int i=0 ;i < persistenceUnits .length; i++) { 66 String title = persistenceUnits[i].getName(); 67 if (isEmpty(title)) { 68 Error.ErrorLocation loc = new Error.ErrorLocation (persistenceUnits[i], "name"); 69 errors.add(new Error (Error.MISSING_VALUE_MESSAGE, "name", loc)); 70 } 71 for (int j = 0; j < persistenceUnits.length; j++) { 72 String tit = persistenceUnits[j].getName(); 73 if (!isEmpty(title) && i != j && title.equals(tit)) { 74 Error.ErrorLocation loc = new Error.ErrorLocation (persistenceUnits[i], "name"); 75 errors.add(new Error (Error.TYPE_FATAL, Error.DUPLICATE_VALUE_MESSAGE, title, loc)); 76 } 77 } 78 } 79 } 80 81 84 private void validateExcludeUnlisted(){ 85 if (!isJavaSE()){ 86 return; 87 } 88 PersistenceUnit[] persistenceUnits = puDataObject.getPersistence().getPersistenceUnit(); 89 for (int i=0 ;i < persistenceUnits .length; i++) { 90 if (persistenceUnits[i].isExcludeUnlistedClasses()){ 91 Error.ErrorLocation loc = new Error.ErrorLocation (persistenceUnits[i], "exclude-unlisted-classes"); 92 errors.add(new Error (Error.TYPE_FATAL, Error.WARNING_MESSAGE, "exclude-unlisted-classes property is not supported in Java SE environments", loc)); 93 } 94 } 95 } 96 97 100 private void validateJarFiles(){ 101 if (!isJavaSE()){ 102 return; 103 } 104 PersistenceUnit[] persistenceUnits = puDataObject.getPersistence().getPersistenceUnit(); 105 for (int i=0 ;i < persistenceUnits .length; i++) { 106 if (persistenceUnits[i].getJarFile() != null && persistenceUnits[i].getJarFile().length > 0){ 107 Error.ErrorLocation loc = new Error.ErrorLocation (persistenceUnits[i], "jar-files"); 108 errors.add(new Error (Error.TYPE_FATAL, Error.WARNING_MESSAGE, "jar-files property is not supported in Java SE environments", loc)); 109 } 110 } 111 112 } 113 114 117 protected boolean isJavaSE(){ 118 Project project = FileOwnerQuery.getOwner(puDataObject.getPrimaryFile()); 119 return Util.isJavaSE(project); 120 } 121 122 private boolean isEmpty(String str){ 123 return null == str || "".equals(str.trim()); 124 } 125 126 } 127 | Popular Tags |