Package com.adaptrex.core.persistence.cayenne

Source Code of com.adaptrex.core.persistence.cayenne.CayenneAssociationType

/*
* Copyright 2012 Adaptrex, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.adaptrex.core.persistence.cayenne;

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

import org.apache.cayenne.ObjectContext;
import org.apache.cayenne.ObjectId;
import org.apache.cayenne.lifecycle.id.IdCoder;
import org.apache.cayenne.map.ObjEntity;
import org.apache.cayenne.map.ObjRelationship;
import org.apache.cayenne.query.ObjectIdQuery;
import org.apache.commons.lang3.StringUtils;

import com.adaptrex.core.ext.ExtTypeFormatter;
import com.adaptrex.core.ext.FieldDefinition;
import com.adaptrex.core.persistence.api.AdaptrexAssociationType;
import com.adaptrex.core.persistence.api.AdaptrexEntityType;
import com.adaptrex.core.security.SecureEntity;
import com.adaptrex.core.utilities.StringUtilities;

public class CayenneAssociationType extends AdaptrexAssociationType {

  private Method getter;
  private Method setter;
 
  public CayenneAssociationType(ObjEntity objEntity, ObjRelationship relationship, Class<?> associationType, SecureEntity secureEntity) {
    super(relationship.getName(), associationType, secureEntity);
    this.idFieldDefinition =
        new FieldDefinition(
            relationship.getName() + StringUtils.capitalize(AdaptrexEntityType.ENTITY_ID_NAME),
            ExtTypeFormatter.STRING
          );
   
    String capName = StringUtilities.capitalize(getName());
   
    Class<?> declaringClass = objEntity.getJavaClass();
    try {
      getter = declaringClass.getMethod("get" + capName);
    } catch (Exception e) {}
    if (getter != null) {
      try {
        setter = declaringClass.getMethod(
            "set" + capName,
            getter.getReturnType()
          );
      } catch (Exception e) {}
    }
  }
 
  @Override
  public Object getAssociationFrom(Object entity) throws Exception {
    return getter.invoke(entity);
  }
 
  public void setById(ObjectContext context, Object entity, Object foreignId) throws Exception {
    IdCoder idCoder = new IdCoder(context.getEntityResolver());
    ObjectId objectId = idCoder.getObjectId(String.valueOf(foreignId));
    ObjectIdQuery query = new ObjectIdQuery(objectId);
    List<?> list = context.performQuery(query);
    Object newForeignObject = list.size() == 0 ? null : list.get(0);
   
    setter.invoke(entity, newForeignObject);
  }
 
  public void set(Object entity, Object associatedEntity) throws Exception {
    setter.invoke(entity, associatedEntity);
  }
}
TOP

Related Classes of com.adaptrex.core.persistence.cayenne.CayenneAssociationType

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.