+" structureInode must be set");
}
Structure st = StructureCache.getStructureByInode(contentlet.getStructureInode());
if(Structure.STRUCTURE_TYPE_FILEASSET==st.getStructureType()){
if(contentlet.getHost()!=null && contentlet.getHost().equals(Host.SYSTEM_HOST) && (!UtilMethods.isSet(contentlet.getFolder()) || contentlet.getFolder().equals(FolderAPI.SYSTEM_FOLDER))){
DotContentletValidationException cve = new FileAssetValidationException("message.contentlet.fileasset.invalid.hostfolder");
cve.addBadTypeField(st.getFieldVar(FileAssetAPI.HOST_FOLDER_FIELD));
throw cve;
}
boolean fileNameExists = false;
try {
Host host = APILocator.getHostAPI().find(contentlet.getHost(), APILocator.getUserAPI().getSystemUser(), false);
Folder folder = null;
if(UtilMethods.isSet(contentlet.getFolder()))
folder=APILocator.getFolderAPI().find(contentlet.getFolder(), APILocator.getUserAPI().getSystemUser(), false);
else
folder=APILocator.getFolderAPI().findSystemFolder();
String fileName = contentlet.getBinary(FileAssetAPI.BINARY_FIELD)!=null?contentlet.getBinary(FileAssetAPI.BINARY_FIELD).getName():"";
if(UtilMethods.isSet(contentlet.getStringProperty("fileName")))//DOTCMS-7093
fileName = contentlet.getStringProperty("fileName");
if(UtilMethods.isSet(fileName)){
fileNameExists = APILocator.getFileAssetAPI().fileNameExists(host,folder,fileName,contentlet.getIdentifier());
if(!APILocator.getFolderAPI().matchFilter(folder, fileName)) {
DotContentletValidationException cve = new FileAssetValidationException("message.file_asset.error.filename.filters");
cve.addBadTypeField(st.getFieldVar(FileAssetAPI.HOST_FOLDER_FIELD));
throw cve;
}
}
} catch (Exception e) {
if(e instanceof FileAssetValidationException)
throw (FileAssetValidationException)e ;
throw new FileAssetValidationException("Unable to validate field: " + FileAssetAPI.BINARY_FIELD,e);
}
if(fileNameExists){
DotContentletValidationException cve = new FileAssetValidationException("message.contentlet.fileasset.filename.already.exists");
cve.addBadTypeField(st.getFieldVar(FileAssetAPI.HOST_FOLDER_FIELD));
throw cve;
}
}
boolean hasError = false;
DotContentletValidationException cve = new DotContentletValidationException("Contentlets' fields are not valid");
List<Field> fields = FieldsCache.getFieldsByStructureInode(stInode);
Structure structure = StructureCache.getStructureByInode(stInode);
Map<String, Object> conMap = contentlet.getMap();
for (Field field : fields) {
Object o = conMap.get(field.getVelocityVarName());
if(o != null){
if(isFieldTypeString(field)){
if(!(o instanceof String)){
cve.addBadTypeField(field);
Logger.error(this,"A text contentlet must be of type String");
}
}else if(isFieldTypeDate(field)){
if(!(o instanceof Date)){
cve.addBadTypeField(field);
Logger.error(this,"A date contentlet must be of type Date");
}
}else if(isFieldTypeBoolean(field)){
if(!(o instanceof Boolean)){
cve.addBadTypeField(field);
Logger.error(this,"A bool contentlet must be of type Boolean");
}
}else if(isFieldTypeFloat(field)){
if(!(o instanceof Float)){
cve.addBadTypeField(field);
Logger.error(this,"A float contentlet must be of type Float");
}
}else if(isFieldTypeLong(field)){
if(!(o instanceof Long || o instanceof Integer)){
cve.addBadTypeField(field);
Logger.error(this,"A integer contentlet must be of type Long or Integer");
}
// http://jira.dotmarketing.net/browse/DOTCMS-1073
// binary field validation
}else if(isFieldTypeBinary(field)){
if(!(o instanceof java.io.File)){
cve.addBadTypeField(field);
Logger.error(this,"A binary contentlet field must be of type File");
}
}else if(isFieldTypeSystem(field) || isFieldTypeConstant(field)){
}else{
Logger.error(this,"Found an unknown field type : This should never happen!!!");
throw new DotContentletStateException("Unknown field type");
}
}
if (field.isRequired()) {
if(o instanceof String){
String s1 = (String)o;
if(!UtilMethods.isSet(s1.trim()) || (field.getFieldType().equals(Field.FieldType.KEY_VALUE.toString())) && s1.equals("{}")) {
cve.addRequiredField(field);
hasError = true;
continue;
}
}
else if(o instanceof java.io.File){
String s1 = ((java.io.File) o).getPath();
if(!UtilMethods.isSet(s1.trim())||s1.trim().contains("-removed-")) {
cve.addRequiredField(field);
hasError = true;
continue;
}
}
else if(field.getFieldType().equals(Field.FieldType.DATE_TIME.toString())){
if(!UtilMethods.isSet(o)){
if(structure.getExpireDateVar() != null){
if(field.getVelocityVarName().equals(structure.getExpireDateVar())){
if(conMap.get("NeverExpire").equals("NeverExpire")){
continue;
}else{
cve.addRequiredField(field);
hasError = true;
continue;
}
}else{
cve.addRequiredField(field);
hasError = true;
continue;
}
}else{
cve.addRequiredField(field);
hasError = true;
continue;
}
}
}
else if( field.getFieldType().equals(Field.FieldType.CATEGORY.toString()) ) {
if( cats == null || cats.size() == 0 ) {
cve.addRequiredField(field);
hasError = true;
continue;
}
try {
User systemUser = APILocator.getUserAPI().getSystemUser();
if (field.getFieldType().equals(Field.FieldType.CATEGORY.toString())) {
CategoryAPI catAPI = APILocator.getCategoryAPI();
Category baseCat = catAPI.find(field.getValues(), systemUser, false);
List<Category> childrenCats = catAPI.getAllChildren(baseCat, systemUser, false);
boolean found = false;
for(Category cat : childrenCats) {
for(Category passedCat : cats) {
try {
if(passedCat.getInode().equalsIgnoreCase(cat.getInode()))
found = true;
} catch (NumberFormatException e) { }
}
}
if(!found) {
cve.addRequiredField(field);
hasError = true;
continue;
}
}
} catch (DotDataException e) {
throw new DotContentletValidationException("Unable to validate a category field: " + field.getVelocityVarName(), e);
} catch (DotSecurityException e) {
throw new DotContentletValidationException("Unable to validate a category field: " + field.getVelocityVarName(), e);
}
} else if (field.getFieldType().equals(Field.FieldType.HOST_OR_FOLDER.toString())) {
if (!UtilMethods.isSet(contentlet.getHost()) && !UtilMethods.isSet(contentlet.getFolder())) {
cve.addRequiredField(field);
hasError = true;
continue;
}
} else if(!UtilMethods.isSet(o)) {
cve.addRequiredField(field);
hasError = true;
continue;
}
if(field.getFieldType().equals(Field.FieldType.IMAGE.toString()) || field.getFieldType().equals(Field.FieldType.FILE.toString())){
if(o instanceof Number){
Number n = (Number)o;
if(n.longValue() == 0){
cve.addRequiredField(field);
hasError = true;
continue;
}
}else if(o instanceof String){
String s = (String)o;
if(s.trim().equals("0")){
cve.addRequiredField(field);
hasError = true;
continue;
}
}
//WYSIWYG patch for blank content
}else if(field.getFieldType().equals(Field.FieldType.WYSIWYG.toString())){
if(o instanceof String){
String s = (String)o;
if (s.trim().toLowerCase().equals("<br>")){
cve.addRequiredField(field);
hasError = true;
continue;
}
}
}
}
if(field.isUnique()){
try{
StringBuilder buffy = new StringBuilder();
buffy.append(" +(live:true working:true)");
buffy.append(" +structureInode:" + contentlet.getStructureInode());
buffy.append(" +languageId:" + contentlet.getLanguageId());
buffy.append(" +(working:true live:true)");
if(UtilMethods.isSet(contentlet.getIdentifier())){
buffy.append(" -(identifier:" + contentlet.getIdentifier() + ")");
}
buffy.append(" +" + contentlet.getStructure().getVelocityVarName() + "." + field.getVelocityVarName() + ":\"" + escape(getFieldValue(contentlet, field).toString()) + "\"");
List<ContentletSearch> contentlets = new ArrayList<ContentletSearch>();
try {
contentlets = searchIndex(buffy.toString(), -1, 0, "inode", APILocator.getUserAPI().getSystemUser(), false);
} catch (Exception e) {
Logger.error(this, e.getMessage(),e);
throw new DotContentletValidationException(e.getMessage(),e);
}
int size = contentlets.size();
if(size > 0 && !hasError){
Boolean unique = true;
for (ContentletSearch contentletSearch : contentlets) {
Contentlet c = conFac.find(contentletSearch.getInode());
Map<String, Object> cMap = c.getMap();
Object obj = cMap.get(field.getVelocityVarName());
if(((String) obj).equalsIgnoreCase(((String) o))) { //DOTCMS-7275
unique = false;
break;
}
}
if(!unique) {
if(UtilMethods.isSet(contentlet.getIdentifier())){//DOTCMS-5409
Iterator<ContentletSearch> contentletsIter = contentlets.iterator();
while (contentletsIter.hasNext()) {
ContentletSearch cont = (ContentletSearch) contentletsIter.next();
if(!contentlet.getIdentifier().equalsIgnoreCase(cont.getIdentifier()))
{
cve.addUniqueField(field);
hasError = true;
break;
}
}
}else{
cve.addUniqueField(field);
hasError = true;
break;
}
}
}
} catch (DotDataException e) {
Logger.error(this,"Unable to get contentlets for structure: " + contentlet.getStructure().getName() ,e);
} catch (DotSecurityException e) {
Logger.error(this,"Unable to get contentlets for structure: " + contentlet.getStructure().getName() ,e);
}
}
String dataType = (field.getFieldContentlet() != null) ? field.getFieldContentlet().replaceAll("[0-9]*", "") : "";
if (UtilMethods.isSet(o) && dataType.equals("text")) {
String s = "";
try{
s = (String)o;
}catch (Exception e) {
Logger.error(this,"Unable to get string value for text field in contentlet",e);
continue;
}
if (s.length() > 255) {
hasError = true;
cve.addMaxLengthField(field);
continue;
}
}
String regext = field.getRegexCheck();
if (UtilMethods.isSet(regext)) {
if (UtilMethods.isSet(o)) {
if(o instanceof Number){
Number n = (Number)o;
String s = n.toString();
boolean match = Pattern.matches(regext, s);
if (!match) {
hasError = true;
cve.addPatternField(field);
continue;
}
}else if(o instanceof String && UtilMethods.isSet(((String)o).trim())){
String s = ((String)o).trim();
boolean match = Pattern.matches(regext, s);
if (!match) {
hasError = true;
cve.addPatternField(field);
continue;
}
}
}
}