Package it.hotel.aspects

Source Code of it.hotel.aspects.AddMultiLingualInterceptor

/*
Copyright (C) European Community 2008 - Licensed under the EUPL V.1.0 (http://ec.europa.eu/idabc/en/document/6523)
*/

package it.hotel.aspects;

import it.hotel.controller.locale.ILocaleContainer;
import it.hotel.model.label.Label;
import it.hotel.model.label.manager.ILabelManager;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;


/**
*
*
*/
public class AddMultiLingualInterceptor implements MethodInterceptor{

  protected ILabelManager labelManager;
  protected ILocaleContainer localeContainer;
 
  public ILocaleContainer getLocaleContainer() {
    return localeContainer;
  }

  public void setLocaleContainer(ILocaleContainer localeContainer) {
    this.localeContainer = localeContainer;
  }

  public ILabelManager getLabelManager() {
    return labelManager;
  }

  public void setLabelManager(ILabelManager labelManager) {
    this.labelManager = labelManager;
  }
    
  /**
   * A methodinterceptor for internationalising a hibernate model that wants to be added
   * to the or updated to the database. The method looks for any methods on the class model
   * of the form LocalisedXXXXX where XXXXX is the name of the field to be internationlised.
   * The fields are then read from the given model and added to the label table with
   * the associated language code and name of the field classified by the class.
   *
   * The method leaves the model intact.
   *
   * @param invocation method to be intercepted, typically saveandupdate
   * @return return val of method to be invoked
   */
  @Override
  public Object invoke(MethodInvocation invocation) throws Throwable {
    String Locale = localeContainer.getLocale();
    Object model = (Object) invocation.getArguments()[0];
   
    List<String> localisedFieldNames = MultiLingualInterceptor.getLocalisedFields(model);
    HashMap<String, String> fieldValues = new HashMap<String,String>();
   
    for (String field: localisedFieldNames){
     
      String methodGetter = "get" + field;
      Method getLocalisedFieldText = model.getClass().getMethod(methodGetter);
      String text = (String) getLocalisedFieldText.invoke(model);
      fieldValues.put(field, text);
      String descriptor = MultiLingualInterceptor.createDescriptorCode(model, field);
     
    }
   
    Object rval = null;
      try {
        rval = invocation.proceed();
        for (String field: localisedFieldNames){
          String text = fieldValues.get(field);
          String descriptor = MultiLingualInterceptor.createDescriptorCode(model, field);
         
          Label label = createLabel(descriptor, model);
          label.setText(text);
          labelManager.add(label);
        }
      } catch (Throwable t) {
       
      }
        return rval;
  }

  private Label createLabel(String descriptor, Object model) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    Label label = null;
    int id = MultiLingualInterceptor.getIdFromModel(model);
   
    String locale = localeContainer.getLocale();
    label= labelManager.getLabel(id, descriptor, locale);
    if (label == null)
    {
      label = new Label();
      label.setCode(descriptor);
      label.setLanguage(locale);
      label.setOwnerId(id);
    }
    return label;
  }

 
 
 

}
TOP

Related Classes of it.hotel.aspects.AddMultiLingualInterceptor

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.