Package br.net.woodstock.rockframework.web.struts2.util

Source Code of br.net.woodstock.rockframework.web.struts2.util.EntityInterceptor

/*
* This file is part of rockframework.
*
* rockframework is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* rockframework is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>;.
*/
package br.net.woodstock.rockframework.web.struts2.util;

import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;

import ognl.NoSuchPropertyException;
import ognl.Ognl;
import ognl.OgnlException;
import br.net.woodstock.rockframework.core.RockFrameworkLogger;
import br.net.woodstock.rockframework.core.RockFrameworkVersion;
import br.net.woodstock.rockframework.core.reflection.BeanDescriptor;
import br.net.woodstock.rockframework.core.reflection.PropertyDescriptor;
import br.net.woodstock.rockframework.core.reflection.impl.BeanDescriptorBuilder;
import br.net.woodstock.rockframework.core.utils.Conditions;
import br.net.woodstock.rockframework.domain.Entity;
import br.net.woodstock.rockframework.web.struts2.AbstractInterceptor;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;

public class EntityInterceptor extends AbstractInterceptor {

  private static final long  serialVersionUID  = RockFrameworkVersion.VERSION;

  private static final String  ENTITY_ID_REGEX    = "^.*(\\..*)?\\.id";

  private static final String  ENTITY_ID      = "id";

  private static final String  ENTITY_SEPARATOR  = ".";

  public EntityInterceptor() {
    super();
  }

  @Override
  public String intercept(final ActionInvocation invocation) throws Exception {
    Object action = invocation.getAction();
    ActionContext ac = invocation.getInvocationContext();
    Map<String, Object> parameters = ac.getParameters();
    for (Entry<String, Object> entry : parameters.entrySet()) {
      String key = entry.getKey();
      if (entry.getValue() != null) {
        String value = null;

        Class<?> type = entry.getValue().getClass().getComponentType();

        if (type == String.class) {
          String[] values = (String[]) entry.getValue();

          if ((values != null) && (values.length == 1)) {
            value = values[0];
          }
        }

        if ((Conditions.isNotEmpty(value)) && (this.isIdParameter(key))) {
          String entityName = this.getEntityName(key);
          this.setIdParameter(action, entityName, value);
        }
      }
    }
    return invocation.invoke();
  }

  private void setIdParameter(final Object action, final String entityName, final String value) throws OgnlException {
    try {
      Object obj = Ognl.getValue(entityName, action);
      if ((obj != null) && (obj instanceof Entity)) {
        Entity<?> entity = (Entity<?>) obj;

        BeanDescriptor beanDescriptor = new BeanDescriptorBuilder(entity.getClass()).getBeanDescriptor();

        PropertyDescriptor propertyDescriptor = beanDescriptor.getProperty(EntityInterceptor.ENTITY_ID);
        Class<?> clazz = propertyDescriptor.getType();

        try {
          Constructor<?> contructor = clazz.getConstructor(new Class[] { String.class });
          Object fieldValue = contructor.newInstance(new Object[] { value });

          RockFrameworkLogger.getLogger().info("Setting entity ID " + entityName + "[" + fieldValue + "]");
          propertyDescriptor.setValue(entity, fieldValue);
        } catch (NoSuchMethodException e) {
          RockFrameworkLogger.getLogger().warn("Could not find constructor " + entity.getClass().getCanonicalName() + "(String). Parameter not setted");
        } catch (Exception e) {
          RockFrameworkLogger.getLogger().warn("Error in constructor " + entity.getClass().getCanonicalName() + "(String)");
          RockFrameworkLogger.getLogger().warn(e.getMessage(), e);
        }
      }
    } catch (NoSuchPropertyException e) {
      RockFrameworkLogger.getLogger().debug(e.getMessage(), e);
    }
  }

  private boolean isIdParameter(final String name) {
    return Pattern.matches(EntityInterceptor.ENTITY_ID_REGEX, name);
  }

  private String getEntityName(final String name) {
    return name.substring(0, name.lastIndexOf(EntityInterceptor.ENTITY_SEPARATOR + EntityInterceptor.ENTITY_ID));
  }
}
TOP

Related Classes of br.net.woodstock.rockframework.web.struts2.util.EntityInterceptor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.