001/* 002 * (C) Copyright 2007 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 * $Id: RelationCreationBean.java 19155 2007-05-22 16:19:48Z sfermigier $ 018 */ 019 020package org.nuxeo.ecm.platform.relations.web.listener.ejb; 021 022import java.util.Locale; 023import java.util.Map; 024 025import javax.faces.application.FacesMessage; 026import javax.faces.component.UIComponent; 027import javax.faces.component.UIInput; 028import javax.faces.context.FacesContext; 029import javax.faces.validator.ValidatorException; 030 031import org.apache.commons.lang.StringUtils; 032import org.apache.commons.logging.Log; 033import org.apache.commons.logging.LogFactory; 034import org.nuxeo.common.utils.i18n.I18NUtils; 035 036/** 037 * Helper for creation form validation and display. 038 * 039 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> 040 * @author <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a> 041 */ 042public class RelationCreationBean { 043 044 private static final Log log = LogFactory.getLog(RelationCreationBean.class); 045 046 public void validateObject(FacesContext context, UIComponent component, Object value) { 047 Map<String, Object> attributes = component.getAttributes(); 048 final String objectTypeInputId = (String) attributes.get("objectTypeInputId"); 049 final String objectLiteralValueInputId = (String) attributes.get("objectLiteralValueInputId"); 050 final String objectUriInputId = (String) attributes.get("objectUriInputId"); 051 final String objectDocumentUidInputId = (String) attributes.get("objectDocumentUidInputId"); 052 053 if (StringUtils.isBlank(objectTypeInputId) || StringUtils.isBlank(objectLiteralValueInputId) 054 || StringUtils.isBlank(objectUriInputId) || StringUtils.isBlank(objectDocumentUidInputId)) { 055 log.error("Cannot validate relation creation: input id(s) not found"); 056 return; 057 } 058 059 final UIInput objectTypeInput = (UIInput) component.findComponent(objectTypeInputId); 060 final UIInput objectLiteralValueInput = (UIInput) component.findComponent(objectLiteralValueInputId); 061 final UIInput objectUriInput = (UIInput) component.findComponent(objectUriInputId); 062 final UIInput objectDocumentUidInput = (UIInput) component.findComponent(objectDocumentUidInputId); 063 064 if (objectTypeInput == null || objectLiteralValueInput == null || objectUriInput == null 065 || objectDocumentUidInput == null) { 066 log.error("Cannot validate relation creation: input(s) not found"); 067 return; 068 } 069 070 FacesMessage message; 071 String objectType = (String) objectTypeInput.getLocalValue(); 072 String objectValue = null; 073 String bundleName = context.getApplication().getMessageBundle(); 074 Locale locale = context.getViewRoot().getLocale(); 075 String msg; 076 077 if (objectType == null) { 078 msg = I18NUtils.getMessageString(bundleName, "error.relation.required.object.type", null, locale); 079 message = new FacesMessage(msg); 080 } else if (objectType.equals("literal")) { 081 objectValue = StringUtils.trim((String) objectLiteralValueInput.getLocalValue()); 082 msg = I18NUtils.getMessageString(bundleName, "error.relation.required.object.text", null, locale); 083 message = new FacesMessage(msg); 084 } else if (objectType.equals("uri")) { 085 // XXX maybe perform better validation on uri 086 objectValue = StringUtils.trim((String) objectUriInput.getLocalValue()); 087 msg = I18NUtils.getMessageString(bundleName, "error.relation.required.object.uri", null, locale); 088 message = new FacesMessage(msg); 089 } else if (objectType.equals("document")) { 090 objectValue = StringUtils.trim((String) objectDocumentUidInput.getLocalValue()); 091 msg = I18NUtils.getMessageString(bundleName, "error.relation.required.object.document", null, locale); 092 message = new FacesMessage(msg); 093 } else { 094 msg = I18NUtils.getMessageString(bundleName, "error.relation.invalid.object.type", null, locale); 095 message = new FacesMessage(msg); 096 } 097 098 if (objectValue == null || objectValue.length() == 0) { 099 message.setSeverity(FacesMessage.SEVERITY_ERROR); 100 throw new ValidatorException(message); 101 } 102 } 103 104}