001/*
002 * (C) Copyright 2007 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id: RelationCreationBean.java 19155 2007-05-22 16:19:48Z sfermigier $
020 */
021
022package org.nuxeo.ecm.platform.relations.web.listener.ejb;
023
024import java.util.Locale;
025import java.util.Map;
026
027import javax.faces.application.FacesMessage;
028import javax.faces.component.UIComponent;
029import javax.faces.component.UIInput;
030import javax.faces.context.FacesContext;
031import javax.faces.validator.ValidatorException;
032
033import org.apache.commons.lang3.StringUtils;
034import org.apache.commons.logging.Log;
035import org.apache.commons.logging.LogFactory;
036import org.nuxeo.common.utils.i18n.I18NUtils;
037
038/**
039 * Helper for creation form validation and display.
040 *
041 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
042 * @author <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a>
043 */
044public class RelationCreationBean {
045
046    private static final Log log = LogFactory.getLog(RelationCreationBean.class);
047
048    public void validateObject(FacesContext context, UIComponent component, Object value) {
049        Map<String, Object> attributes = component.getAttributes();
050        final String objectTypeInputId = (String) attributes.get("objectTypeInputId");
051        final String objectLiteralValueInputId = (String) attributes.get("objectLiteralValueInputId");
052        final String objectUriInputId = (String) attributes.get("objectUriInputId");
053        final String objectDocumentUidInputId = (String) attributes.get("objectDocumentUidInputId");
054
055        if (StringUtils.isBlank(objectTypeInputId) || StringUtils.isBlank(objectLiteralValueInputId)
056                || StringUtils.isBlank(objectUriInputId) || StringUtils.isBlank(objectDocumentUidInputId)) {
057            log.error("Cannot validate relation creation: input id(s) not found");
058            return;
059        }
060
061        final UIInput objectTypeInput = (UIInput) component.findComponent(objectTypeInputId);
062        final UIInput objectLiteralValueInput = (UIInput) component.findComponent(objectLiteralValueInputId);
063        final UIInput objectUriInput = (UIInput) component.findComponent(objectUriInputId);
064        final UIInput objectDocumentUidInput = (UIInput) component.findComponent(objectDocumentUidInputId);
065
066        if (objectTypeInput == null || objectLiteralValueInput == null || objectUriInput == null
067                || objectDocumentUidInput == null) {
068            log.error("Cannot validate relation creation: input(s) not found");
069            return;
070        }
071
072        FacesMessage message;
073        String objectType = (String) objectTypeInput.getLocalValue();
074        String objectValue = null;
075        String bundleName = context.getApplication().getMessageBundle();
076        Locale locale = context.getViewRoot().getLocale();
077        String msg;
078
079        if (objectType == null) {
080            msg = I18NUtils.getMessageString(bundleName, "error.relation.required.object.type", null, locale);
081            message = new FacesMessage(msg);
082        } else if (objectType.equals("literal")) {
083            objectValue = StringUtils.trim((String) objectLiteralValueInput.getLocalValue());
084            msg = I18NUtils.getMessageString(bundleName, "error.relation.required.object.text", null, locale);
085            message = new FacesMessage(msg);
086        } else if (objectType.equals("uri")) {
087            // XXX maybe perform better validation on uri
088            objectValue = StringUtils.trim((String) objectUriInput.getLocalValue());
089            msg = I18NUtils.getMessageString(bundleName, "error.relation.required.object.uri", null, locale);
090            message = new FacesMessage(msg);
091        } else if (objectType.equals("document")) {
092            objectValue = StringUtils.trim((String) objectDocumentUidInput.getLocalValue());
093            msg = I18NUtils.getMessageString(bundleName, "error.relation.required.object.document", null, locale);
094            message = new FacesMessage(msg);
095        } else {
096            msg = I18NUtils.getMessageString(bundleName, "error.relation.invalid.object.type", null, locale);
097            message = new FacesMessage(msg);
098        }
099
100        if (objectValue == null || objectValue.length() == 0) {
101            message.setSeverity(FacesMessage.SEVERITY_ERROR);
102            throw new ValidatorException(message);
103        }
104    }
105
106}