001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.core.cmp.cmp2;
018:
019: import org.apache.openejb.core.CoreDeploymentInfo;
020:
021: import javax.ejb.EJBException;
022: import javax.ejb.EJBLocalObject;
023: import javax.ejb.EntityBean;
024:
025: //
026: // WARNING: Do not refactor this class. It is used by the Cmp2Generator.
027: //
028: public class SingleValuedCmr<Bean extends EntityBean, Proxy extends EJBLocalObject> {
029: private final EntityBean source;
030: private final String sourceProperty;
031: private final String relatedProperty;
032: private final CoreDeploymentInfo relatedInfo;
033:
034: public SingleValuedCmr(EntityBean source, String sourceProperty,
035: Class<Bean> relatedType, String relatedProperty) {
036: if (source == null)
037: throw new NullPointerException("source is null");
038: if (relatedType == null)
039: throw new NullPointerException("relatedType is null");
040: this .source = source;
041: this .sourceProperty = sourceProperty;
042: this .relatedProperty = relatedProperty;
043:
044: this .relatedInfo = Cmp2Util.getDeploymentInfo(relatedType);
045: }
046:
047: public Proxy get(Bean entity) throws EJBException {
048: if (sourceProperty == null) {
049: throw new EJBException(
050: "Internal error: this container managed relationship is unidirectional and, "
051: + "this entity does not have a cmr field for the relationship");
052: }
053:
054: if (entity == null)
055: return null;
056:
057: Proxy ejbProxy = Cmp2Util.<Proxy> getEjbProxy(relatedInfo,
058: entity);
059: return ejbProxy;
060: }
061:
062: public Bean set(Bean oldBean, Proxy newValue) throws EJBException {
063: Bean newBean = Cmp2Util.<Bean> getEntityBean(newValue);
064: if (newValue != null && newBean == null) {
065: // todo verify that this is the only way null can be returned
066: throw new IllegalArgumentException(
067: "A deleted bean can not be assigned to a relationship");
068: }
069:
070: if (relatedProperty != null) {
071: // clear back reference in the old related bean
072: if (oldBean != null) {
073: toCmp2Entity(oldBean).OpenEJB_removeCmr(
074: relatedProperty, source);
075: }
076:
077: if (newValue != null) {
078: // set the back reference in the new related bean
079: Object oldBackRef = toCmp2Entity(newBean)
080: .OpenEJB_addCmr(relatedProperty, source);
081:
082: // if the new related beas was related to another bean, we need
083: // to clear the back reference in that old bean
084: if (oldBackRef != null) {
085: toCmp2Entity(oldBackRef).OpenEJB_removeCmr(
086: sourceProperty, newBean);
087: }
088: }
089: }
090: return newBean;
091: }
092:
093: public void deleted(Bean oldBean) throws EJBException {
094: set(oldBean, null);
095: }
096:
097: private Cmp2Entity toCmp2Entity(Object object) {
098: return (Cmp2Entity) object;
099: }
100: }
|