ola, estou tentando utilizar flex + java usando n-camadas mais nao estou obtendo sucesso
Eu tenho uma classe session que é responsavel pelo serviço (salvar,excluir e tudo mais, manipula as minhas entitys, encapsula negocio)
Tenho a dao responsavel pela persistencia, a query responsavel por gerar SQL, e a factory responsavel por instanciar objetos.
Ok ai esta a minha arquitetura. O problema é o seguinte quando manda salvar a marca no flex o objeto vem até a minha DAO mais nao persiste.
no lado do java a minha SessionMarca herda de Session.
e quando mando carregar minha dataGrid ele gera o sql errado.
*** Tbm Estou em duvida se devo implementar as mesmas coisas do lado do flex?
Meu add.mxml
[code]<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx=“http://www.adobe.com/2006/mxml” layout=“absolute” creationComplete=“find()”>
<mx:RemoteObject id=“serviceMarca” destination=“sessionMarca” channelSet="{utils.Utils.getChannelSet()}" fault=“onFault(event)”>
<mx:method name=“save” result=“onResultSave(event)”/>
<mx:method name=“deletar” result=“onResultDeletar(event)”/>
<mx:method name=“find” result=“onResultFind(event)”/>
<mx:method name=“findByPrimaryKey” result=“onResultFindByPrimaryKey(event)”/>
</mx:RemoteObject>
mx:Script
<![CDATA[
import session.SessionMarca;
import utils.Utils;
import entity.Marca;
import mx.controls.Alert;
import mx.collections.ArrayCollection;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
private var marca:Marca;
private var sessinoMarca:SessionMarca;
[Bindable]
private var entity:Array;
private function save():void{
this.marca = new Marca();
marca.descricao = txtDescricaoMarca.text;
serviceMarca.save(marca,false);
}
private function deletar():void{
if(dgMarca.selectedItem != null)
serviceMarca.deletar(dgMarca.selectedItem as Marca,false);
}
private function find():void{
this.marca = new Marca();
serviceMarca.find(marca);
}
private function findByPrimaryKey():void{
this.marca = new Marca();
serviceMarca.findByPrimaryKey(marca);
}
private function onResultSave(event:ResultEvent):void{
find();
}
private function onResultDeletar(event:ResultEvent):void{
find();
}
/*private function onResultEditarData(event:ResultEvent):void{
getData();
}*/
private function onResultFind(event:ResultEvent):void{
entity = event.result as Array;
}
private function onResultFindByPrimaryKey(event:ResultEvent):void{
this.marca = event.result as Marca;
}
private function onFault(event:FaultEvent):void{
Alert.show("Deu erro","Error");
}
]]>
</mx:Script>
<mx:TabNavigator width="717" height="315" backgroundColor="#C9C1C1" horizontalCenter="0" verticalCenter="-22">
<mx:Canvas label="Marca" width="100%" height="100%" id="marcaShow" borderStyle="solid" backgroundColor="#7E7C7C">
<mx:TextInput x="202.5" y="168" id="txtDescricaoMarca" maxChars="30" width="308"/>
<mx:Label x="141" y="170" text="Descrição:"/>
<mx:DataGrid id="dgMarca" height="143" width="369" editable="true" y="10" x="141" dataProvider="{entity}">
<mx:columns>
<mx:DataGridColumn headerText="ID" dataField="dgId" width="40"/>
<mx:DataGridColumn headerText="Descrição" dataField="dgDescricao"/>
</mx:columns>
</mx:DataGrid>
<mx:Button x="141" y="214" label="Adicionar" id="btnAddMarca" click="save()"/>
<mx:Button x="229" y="214" label="Excluir" id="btnExcluirMarca" click="deletar()"/>
<mx:Button x="301" y="214" label="Editar" id="btnEditarMarca"/>
</mx:Canvas>
<mx:/TabNavigator>
</Application>[/code]
Session.java
[code]package session;
import dao.Dao;
import entity.Entity;
import entity.Marca;
public abstract class Session {
protected Dao dao;
public Session(){
}
//bTransaction informa se existe ou não a transação
public void deletar(Entity objeto, boolean bTransaction) throws Exception {
try {
if (!bTransaction) {
//Abrir a transação
this.dao.beginTransaction();
}
this.dao.delete(objeto);
if (!bTransaction) {
//Fechar a transação
this.dao.commit();
}
} catch (Exception e) {
if (!bTransaction) {
//Fechar a transação
this.dao.rollback();
}
throw e;
}
}
//bTransaction informa se existe ou não a transação
public void save(Entity objeto, boolean bTransaction) throws Exception {
try {
if (!bTransaction) {
//Abrir a transação
this.dao.beginTransaction();
}
System.out.println(((Marca)objeto).getDescricao());
this.dao.save(objeto);
if (!bTransaction) {
//Fechar a transação
this.dao.commit();
}
} catch (Exception e) {
if (!bTransaction) {
//Fechar a transação
this.dao.rollback();
}
throw e;
}
}
public Entity[] find(Entity objeto) throws Exception {
return this.dao.find(objeto);
}
public Entity findByPrimaryKey(Entity objeto)throws Exception{
return this.dao.findByPrimaryKey(objeto);
}
}
[/code]
Dao.Java
[code]package dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import util.;
import entity.;
import entity.Entity;
import entity.Produto;
public abstract class Dao {
protected Query query;
protected Database database;
protected Factory factory;
public Dao() {
this.query = new Query();
this.database = Database.getInstance();
this.factory = Factory.getInstance();
}
public void save(Entity objeto)throws Exception {
try {
System.out.println(((Marca)objeto).getDescricao() + "Aqui eh na dao ::");
String sql;
Statement stmt = this.database.getConnection().createStatement();
if ((objeto.getIdValue() == null) ||
(objeto.getIdValue().equals("null")) ||
(objeto.getIdValue().equals(""))) {
System.out.println(((Marca)objeto).getDescricao() +" depois if dao");
sql = this.query.getSqlInsert(objeto);
} else {
System.out.println(((Marca)objeto).getDescricao() +" else");
sql = this.query.getSqlUpdate(objeto);
}
stmt.executeUpdate(sql,Statement.RETURN_GENERATED_KEYS);
ResultSet rst = stmt.getGeneratedKeys();
if (rst.next()) {
objeto.setIdValue(rst.getString(1));
}
stmt.close();
} catch (SQLException e) {
throw e;
}
}
public void delete(Entity objeto) throws Exception {
try {
String sql;
sql = this.query.getSqlDelete(objeto);
Statement stmt = this.database.getConnection().createStatement();
stmt.executeUpdate(sql);
stmt.close();
} catch (SQLException e) {
throw e;
}
}
public Entity findByPrimaryKey(Entity objeto)throws Exception {
String values[] = new String[objeto.getFieldNames().length];
values[0] = objeto.getIdValue();
Entity listaRetorno[] = this.find(this.factory.createByArray(objeto.getClass().getName(),values));
if ((listaRetorno != null) &&
(listaRetorno.length > 0)) {
return listaRetorno[0];
}
return null;
}
public Entity[] find(Entity objeto) throws Exception {
Entity[] retorno = null;
try {
String sql = this.query.getSqlSelect(objeto);
System.out.println(sql);
Statement stmt = this.database.getConnection().createStatement();
ResultSet rst = stmt.executeQuery(sql);
retorno = this.factory.createByResultSet(objeto.getClass().getName(), rst);
} catch (SQLException e) {
throw e;
}
return retorno;
}
public ResultSet find(String sql) throws Exception{
try {
Statement stmt = this.database.getConnection().createStatement();
return stmt.executeQuery(sql);
}
catch (SQLException e) {
throw e;
}
}
public void beginTransaction() throws SQLException {
this.database.getConnection().setAutoCommit(false);
}
public void commit() throws SQLException {
this.database.getConnection().commit();
}
public void rollback() throws SQLException {
this.database.getConnection().rollback();
}
[/code]
Remoting-config.xml
[code]<?xml version="1.0" encoding="UTF-8"?>
<adapters>
<adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>
</adapters>
<default-channels>
<channel ref="my-amf"/>
</default-channels>
<destination id="sessionMarca">
<properties>
<source>session.SessionMarca</source>
</properties>
</destination>[/code]
Desde já agradeço a atençao e a ajuda!!!