private void generateWriteAttributeMethod(JCodeModel jModel, JPackage jPackage, JDefinedClass jClusterClass,
			ZclClusterDescriptor zclClusterDescriptor, boolean isServer) throws Exception {

		Vector zclAttributeDescriptors = null;
		if (isServer)
			zclAttributeDescriptors = zclClusterDescriptor.getZclClientAttributesDescriptors();
		else
			zclAttributeDescriptors = zclClusterDescriptor.getZclServerAttributesDescriptors();

		if (zclAttributeDescriptors.size() == 0)
			return;

		JClass jZCLClass = jModel.ref(ZCL.class);

		JMethod jWriteAttibutesMethod = jClusterClass.method(JMod.PROTECTED, short.class, "writeAttribute");
		JVar jZclFrameVar = jWriteAttibutesMethod.param(IZclFrame.class, "zclFrame");
		JVar jAttrIdVar = jWriteAttibutesMethod.param(int.class, "attrId");
		JVar jDataTypeVar = jWriteAttibutesMethod.param(short.class, "dataType");
		jWriteAttibutesMethod._throws(Exception.class);

		JBlock jBlock = jWriteAttibutesMethod.body();

		JClass interfaceClass = getClusterConnectionClass(zclClusterDescriptor, !isServer);
		if (interfaceClass == null) {
			return;
		}

		JVar jVarPeer = null;

		int rwAttributes = 0;
		for (int i = 0; i < zclAttributeDescriptors.size(); i++) {
			ZclAttributeDescr zclAttributeDescriptor = (ZclAttributeDescr) zclAttributeDescriptors.get(i);

			if (zclAttributeDescriptor.getAccess() == ZclAttributeDescr.ACCESS_RW) {
				rwAttributes++;
			}
		}

		if (rwAttributes > 0) {
			JInvocation jInvocation = JExpr.invoke("getSinglePeerCluster").arg(
					JExpr.direct(interfaceClass.name() + ".class.getName()"));
			jVarPeer = jBlock.decl(interfaceClass, "c").init(JExpr.cast(interfaceClass, jInvocation));
		}

		JSwitch jSwitch = jBlock._switch(jAttrIdVar);

		JBlock jCaseBody = null;

		// Read Only Attributes
		int roAttributes = 0;
		for (int i = 0; i < zclAttributeDescriptors.size(); i++) {
			ZclAttributeDescr zclAttributeDescriptor = (ZclAttributeDescr) zclAttributeDescriptors.get(i);

			if (zclAttributeDescriptor.getAccess() == ZclAttributeDescr.ACCESS_RO) {
				jCaseBody = jSwitch._case(JExpr.lit(zclAttributeDescriptor.getId())).body();
				roAttributes++;
			}
		}

		if (roAttributes > 0)
			jCaseBody._return(jZCLClass.staticRef("READ_ONLY"));

		for (int i = 0; i < zclAttributeDescriptors.size(); i++) {
			ZclAttributeDescr zclAttributeDescriptor = (ZclAttributeDescr) zclAttributeDescriptors.get(i);

			// issue the calls
			if (zclAttributeDescriptor.getAccess() == ZclAttributeDescr.ACCESS_RW) {
				jCaseBody = jSwitch._case(JExpr.lit(zclAttributeDescriptor.getId())).body();

				JType type = jTypeForZbTypename(jModel, jClusterClass.getPackage(), zclAttributeDescriptor.getTypeName(),
						zclAttributeDescriptor);

				JClass jClassZigBeeType = getZclDataTypeJClass(jModel, jClusterClass.getPackage(), jClusterClass,
						zclAttributeDescriptor.getTypeName(), zclAttributeDescriptor);

				JVar param = jCaseBody.decl(type, "v");
				param.init(jClassZigBeeType.staticInvoke("zclParse").arg(jZclFrameVar));

				JInvocation jSetterInvocation = jCaseBody.invoke(jVarPeer, this.attributeSetterName(zclAttributeDescriptor)).arg(
						param);
				if (useDefaultRequestContext) {
					jSetterInvocation.arg(JExpr.ref("endPoint").invoke("getDefaultRequestContext"));
				} else {
					jSetterInvocation.arg(JExpr._null());
				}

				jCaseBody._break();
			}
		}

		// Return false since the notify didn't handle the incoming message.
		jSwitch._default().body()._return(jZCLClass.staticRef("UNSUPPORTED_ATTRIBUTE"));

		// jWriteAttibutesMethod.body()._return(jZCLClass.staticRef("SUCCESS"));
	}