1
0
mirror of https://github.com/Rogiel/l2jserver2 synced 2025-12-06 07:32:46 +00:00

Merge branch 'proposed/database-derby'

Conflicts:
	pom.xml
This commit is contained in:
2011-10-06 01:03:26 -03:00
35 changed files with 1114 additions and 365 deletions

View File

@@ -0,0 +1,57 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.database;
import com.google.inject.AbstractModule;
import com.google.inject.Module;
import com.google.inject.Scopes;
import com.l2jserver.model.dao.CharacterDAO;
import com.l2jserver.model.dao.CharacterFriendDAO;
import com.l2jserver.model.dao.ChatMessageDAO;
import com.l2jserver.model.dao.ClanDAO;
import com.l2jserver.model.dao.ItemDAO;
import com.l2jserver.model.dao.NPCDAO;
import com.l2jserver.service.database.jdbc.derby.DerbyCharacterDAO;
import com.l2jserver.service.database.jdbc.derby.DerbyCharacterFriendDAO;
import com.l2jserver.service.database.jdbc.derby.DerbyChatMessageDAO;
import com.l2jserver.service.database.jdbc.derby.DerbyClanDAO;
import com.l2jserver.service.database.jdbc.derby.DerbyItemDAO;
import com.l2jserver.service.database.jdbc.derby.DerbyNPCDAO;
/**
* Google Guice {@link Module} for Derby DAOs
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class DerbyDAOModule extends AbstractModule {
@Override
protected void configure() {
bind(CharacterDAO.class).to(DerbyCharacterDAO.class).in(
Scopes.SINGLETON);
bind(CharacterFriendDAO.class).to(DerbyCharacterFriendDAO.class).in(
Scopes.SINGLETON);
bind(NPCDAO.class).to(DerbyNPCDAO.class).in(Scopes.SINGLETON);
bind(ItemDAO.class).to(DerbyItemDAO.class).in(Scopes.SINGLETON);
bind(ClanDAO.class).to(DerbyClanDAO.class).in(Scopes.SINGLETON);
// logs
bind(ChatMessageDAO.class).to(DerbyChatMessageDAO.class).in(
Scopes.SINGLETON);
}
}

View File

@@ -0,0 +1,47 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.database;
import com.google.inject.AbstractModule;
import com.google.inject.Module;
import com.google.inject.Scopes;
import com.l2jserver.model.dao.CharacterDAO;
import com.l2jserver.service.database.orientdb.OrientDBCharacterDAO;
/**
* Google Guice {@link Module} for Orient Database DAOs
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class OrientDBDAOModule extends AbstractModule {
@Override
protected void configure() {
bind(CharacterDAO.class).to(OrientDBCharacterDAO.class).in(
Scopes.SINGLETON);
// bind(CharacterFriendDAO.class).to(OrientDBCharacterFriendDAO.class).in(
// Scopes.SINGLETON);
//
// bind(NPCDAO.class).to(OrientDBNPCDAO.class).in(Scopes.SINGLETON);
//
// bind(ItemDAO.class).to(OrientDBItemDAO.class).in(Scopes.SINGLETON);
// bind(ClanDAO.class).to(OrientDBClanDAO.class).in(Scopes.SINGLETON);
//
// // logs
// bind(ChatMessageDAO.class).to(OrientDBChatMessageDAO.class).in(
// Scopes.SINGLETON);
}
}

View File

@@ -0,0 +1,55 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.database.jdbc.derby;
import com.google.inject.Inject;
import com.l2jserver.model.dao.CharacterDAO;
import com.l2jserver.model.id.object.provider.CharacterIDProvider;
import com.l2jserver.model.id.object.provider.ClanIDProvider;
import com.l2jserver.model.id.provider.AccountIDProvider;
import com.l2jserver.model.id.template.provider.CharacterTemplateIDProvider;
import com.l2jserver.service.database.DatabaseService;
import com.l2jserver.service.database.jdbc.JDBCCharacterDAO;
/**
* {@link CharacterDAO} implementation for MySQL5
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class DerbyCharacterDAO extends JDBCCharacterDAO implements
CharacterDAO {
/**
* @param database
* the database service
* @param idFactory
* the character id provider
* @param templateIdFactory
* the template id provider
* @param accountIdFactory
* the account id provider
* @param clanIdFactory
* the clan id provider
*/
@Inject
public DerbyCharacterDAO(DatabaseService database,
CharacterIDProvider idFactory,
CharacterTemplateIDProvider templateIdFactory,
AccountIDProvider accountIdFactory, ClanIDProvider clanIdFactory) {
super(database, idFactory, templateIdFactory, accountIdFactory,
clanIdFactory);
}
}

View File

@@ -0,0 +1,46 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.database.jdbc.derby;
import com.google.inject.Inject;
import com.l2jserver.model.dao.CharacterFriendDAO;
import com.l2jserver.model.id.object.provider.CharacterIDProvider;
import com.l2jserver.model.id.provider.FriendIDProvider;
import com.l2jserver.service.database.DatabaseService;
import com.l2jserver.service.database.jdbc.JDBCCharacterFriendDAO;
/**
* {@link CharacterFriendDAO} implementation for MySQL5
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class DerbyCharacterFriendDAO extends JDBCCharacterFriendDAO implements
CharacterFriendDAO {
/**
* @param database
* the database service
* @param idProvider
* the frind id provider
* @param charIdProvider
* the character id provider
*/
@Inject
public DerbyCharacterFriendDAO(DatabaseService database,
FriendIDProvider idProvider, CharacterIDProvider charIdProvider) {
super(database, idProvider, charIdProvider);
}
}

View File

@@ -0,0 +1,46 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.database.jdbc.derby;
import com.google.inject.Inject;
import com.l2jserver.model.dao.ChatMessageDAO;
import com.l2jserver.model.id.object.provider.CharacterIDProvider;
import com.l2jserver.model.id.provider.ChatMessageIDProvider;
import com.l2jserver.service.database.DatabaseService;
import com.l2jserver.service.database.jdbc.JDBCChatMessageDAO;
/**
* {@link ChatMessageDAO} implementation for MySQL5
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class DerbyChatMessageDAO extends JDBCChatMessageDAO implements
ChatMessageDAO {
/**
* @param database
* the database service
* @param idFactory
* the chat message id provider
* @param charIdFactory
* the character id provider
*/
@Inject
public DerbyChatMessageDAO(DatabaseService database,
ChatMessageIDProvider idFactory, CharacterIDProvider charIdFactory) {
super(database, idFactory, charIdFactory);
}
}

View File

@@ -14,31 +14,33 @@
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.database.orientdb;
package com.l2jserver.service.database.jdbc.derby;
import com.l2jserver.model.Model;
import com.l2jserver.model.id.ID;
import com.l2jserver.service.database.AbstractDAO;
import com.l2jserver.service.database.AbstractOrientDatabaseService;
import com.google.inject.Inject;
import com.l2jserver.model.dao.CharacterDAO;
import com.l2jserver.model.dao.ClanDAO;
import com.l2jserver.model.id.object.provider.CharacterIDProvider;
import com.l2jserver.model.id.object.provider.ClanIDProvider;
import com.l2jserver.service.database.DatabaseService;
import com.l2jserver.service.database.jdbc.JDBCClanDAO;
/**
* {@link CharacterDAO} implementation for MySQL5
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
* @param <T>
* the model type
* @param <I>
* the id type
*/
public abstract class AbstractOrientDBDAO<T extends Model<?>, I extends ID<?>>
extends AbstractDAO<T, I> {
protected final AbstractOrientDatabaseService database;
public class DerbyClanDAO extends JDBCClanDAO implements ClanDAO {
/**
* @param database
* the database instance
* the database service
* @param clanIdFactory
* the clan id provider
* @param idFactory
* the character id provider
*/
protected AbstractOrientDBDAO(DatabaseService database) {
super(database);
this.database = (AbstractOrientDatabaseService) database;
@Inject
public DerbyClanDAO(DatabaseService database,
ClanIDProvider clanIdFactory, CharacterIDProvider idFactory) {
super(database, clanIdFactory, idFactory);
}
}

View File

@@ -0,0 +1,49 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.database.jdbc.derby;
import com.google.inject.Inject;
import com.l2jserver.model.dao.ItemDAO;
import com.l2jserver.model.id.object.provider.CharacterIDProvider;
import com.l2jserver.model.id.object.provider.ItemIDProvider;
import com.l2jserver.model.id.template.provider.ItemTemplateIDProvider;
import com.l2jserver.service.database.DatabaseService;
import com.l2jserver.service.database.jdbc.JDBCItemDAO;
/**
* {@link ItemDAO} implementation for MySQL5
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class DerbyItemDAO extends JDBCItemDAO implements ItemDAO {
/**
* @param database
* the database service
* @param idFactory
* the item id provider
* @param templateIdFactory
* the item template id provider
* @param charIdFactory
* the character id provider
*/
@Inject
public DerbyItemDAO(DatabaseService database, ItemIDProvider idFactory,
ItemTemplateIDProvider templateIdFactory,
CharacterIDProvider charIdFactory) {
super(database, idFactory, templateIdFactory, charIdFactory);
}
}

View File

@@ -0,0 +1,46 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.database.jdbc.derby;
import com.google.inject.Inject;
import com.l2jserver.model.dao.CharacterDAO;
import com.l2jserver.model.dao.NPCDAO;
import com.l2jserver.model.id.object.provider.NPCIDProvider;
import com.l2jserver.model.id.template.provider.NPCTemplateIDProvider;
import com.l2jserver.service.database.DatabaseService;
import com.l2jserver.service.database.jdbc.JDBCNPCDAO;
/**
* {@link CharacterDAO} implementation for MySQL5
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class DerbyNPCDAO extends JDBCNPCDAO implements NPCDAO {
/**
* @param database
* the database service
* @param idProvider
* the npc id provider
* @param templateIdProvider
* the npc template id provider
*/
@Inject
public DerbyNPCDAO(DatabaseService database, NPCIDProvider idProvider,
NPCTemplateIDProvider templateIdProvider) {
super(database, idProvider, templateIdProvider);
}
}

View File

@@ -0,0 +1,282 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.database.orientdb;
import java.sql.SQLException;
import java.util.List;
import com.google.inject.Inject;
import com.l2jserver.model.dao.CharacterFriendDAO;
import com.l2jserver.model.game.CharacterFriend;
import com.l2jserver.model.id.FriendID;
import com.l2jserver.model.id.object.CharacterID;
import com.l2jserver.model.id.object.provider.CharacterIDProvider;
import com.l2jserver.model.id.provider.FriendIDProvider;
import com.l2jserver.model.world.L2Character;
import com.l2jserver.model.world.character.CharacterFriendList;
import com.l2jserver.service.database.AbstractOrientDatabaseService.CachedMapper;
import com.l2jserver.service.database.AbstractOrientDatabaseService.InsertUpdateQuery;
import com.l2jserver.service.database.AbstractOrientDatabaseService.Mapper;
import com.l2jserver.service.database.AbstractOrientDatabaseService.SelectListQuery;
import com.l2jserver.service.database.AbstractOrientDatabaseService.SelectSingleQuery;
import com.l2jserver.service.database.DatabaseService;
import com.l2jserver.service.database.jdbc.JDBCCharacterDAO;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.query.nativ.ONativeSynchQuery;
import com.orientechnologies.orient.core.query.nativ.OQueryContextNativeSchema;
import com.orientechnologies.orient.core.record.impl.ODocument;
/**
* {@link CharacterFriendDAO} implementation for JDBC
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public abstract class OrientDBCharacterFriendDAO extends
AbstractOrientDBDAO<CharacterFriend, FriendID> implements
CharacterFriendDAO {
/**
* The {@link FriendID} provider
*/
private final FriendIDProvider idProvider;
/**
* The {@link CharacterID} provider
*/
private final CharacterIDProvider charIdProvider;
/**
* Character table name
*/
public static final String CLASS_NAME = CharacterFriend.class
.getSimpleName();
// FIELDS
public static final String CHAR_ID = JDBCCharacterDAO.CHAR_ID;
public static final String CHAR_ID_FRIEND = JDBCCharacterDAO.CHAR_ID
+ "_friend";
/**
* @param database
* the database service
* @param idProvider
* the frind id provider
* @param charIdProvider
* the character id provider
*/
@Inject
public OrientDBCharacterFriendDAO(DatabaseService database,
final FriendIDProvider idProvider,
CharacterIDProvider charIdProvider) {
super(database);
this.idProvider = idProvider;
this.charIdProvider = charIdProvider;
}
/**
* The {@link Mapper} for {@link FriendID}
*/
private final Mapper<FriendID> idMapper = new Mapper<FriendID>() {
@Override
public FriendID map(ODocument document) throws SQLException {
final CharacterID characterId = charIdProvider
.resolveID((Integer) document.field(CHAR_ID));
final CharacterID friendId = charIdProvider
.resolveID((Integer) document.field(CHAR_ID_FRIEND));
return idProvider.createID(characterId, friendId);
}
};
/**
* The {@link Mapper} for {@link CharacterFriend}
*/
private final Mapper<CharacterFriend> mapper = new CachedMapper<CharacterFriend, FriendID>(
database, idMapper) {
@Override
protected CharacterFriend map(FriendID id, ODocument document)
throws SQLException {
return new CharacterFriend(id);
}
};
@Override
public CharacterFriend select(final FriendID id) {
return database.query(new SelectSingleQuery<CharacterFriend>() {
@Override
protected ONativeSynchQuery<ODocument, OQueryContextNativeSchema<ODocument>> createQuery(
ODatabaseDocumentTx database) {
return new ONativeSynchQuery<ODocument, OQueryContextNativeSchema<ODocument>>(
database, CLASS_NAME,
new OQueryContextNativeSchema<ODocument>()) {
private static final long serialVersionUID = 1L;
@Override
public boolean filter(
OQueryContextNativeSchema<ODocument> criteria) {
return criteria.field(CHAR_ID).eq(id.getID1().getID())
.field(CHAR_ID_FRIEND).eq(id.getID2().getID())
.go();
};
};
}
@Override
protected Mapper<CharacterFriend> mapper() {
return mapper;
}
});
}
@Override
public void load(final L2Character character) {
final List<CharacterFriend> list = database
.query(new SelectListQuery<CharacterFriend>() {
@Override
protected ONativeSynchQuery<ODocument, OQueryContextNativeSchema<ODocument>> createQuery(
ODatabaseDocumentTx database) {
return new ONativeSynchQuery<ODocument, OQueryContextNativeSchema<ODocument>>(
database, CLASS_NAME,
new OQueryContextNativeSchema<ODocument>()) {
private static final long serialVersionUID = 1L;
@Override
public boolean filter(
OQueryContextNativeSchema<ODocument> criteria) {
return criteria.field(CHAR_ID)
.eq(character.getID().getID()).go();
};
};
}
@Override
protected Mapper<CharacterFriend> mapper() {
return mapper;
}
});
character.getFriendList().load(list);
}
@Override
public List<FriendID> selectIDs() {
return database.query(new SelectListQuery<FriendID>() {
@Override
protected ONativeSynchQuery<ODocument, OQueryContextNativeSchema<ODocument>> createQuery(
ODatabaseDocumentTx database) {
return new ONativeSynchQuery<ODocument, OQueryContextNativeSchema<ODocument>>(
database, CLASS_NAME,
new OQueryContextNativeSchema<ODocument>()) {
private static final long serialVersionUID = 1L;
@Override
public boolean filter(
OQueryContextNativeSchema<ODocument> criteria) {
return true;
};
};
}
@Override
protected Mapper<FriendID> mapper() {
return idMapper;
}
});
}
@Override
public boolean insert(CharacterFriend friend) {
return database.query(new InsertUpdateQuery<CharacterFriend>(friend) {
@Override
protected ONativeSynchQuery<ODocument, OQueryContextNativeSchema<ODocument>> createQuery(
ODatabaseDocumentTx database, CharacterFriend object) {
return null;
}
@Override
protected ODocument update(ODocument document,
CharacterFriend object) throws SQLException {
return null;
}
@Override
protected ODocument insert(ODocument document,
CharacterFriend friend) throws SQLException {
document.field(CHAR_ID, friend.getCharacterID());
document.field(CHAR_ID_FRIEND, friend.getFriendID());
return document;
}
}) > 0;
}
@Override
public boolean update(CharacterFriend friend) {
// it is not possible update friend objects, because they are only a ID
// pair and IDs are immutable
return false;
}
@Override
public boolean delete(CharacterFriend friend) {
return database.query(new InsertUpdateQuery<CharacterFriend>(friend) {
@Override
protected ONativeSynchQuery<ODocument, OQueryContextNativeSchema<ODocument>> createQuery(
ODatabaseDocumentTx database, final CharacterFriend friend) {
return new ONativeSynchQuery<ODocument, OQueryContextNativeSchema<ODocument>>(
database, CLASS_NAME,
new OQueryContextNativeSchema<ODocument>()) {
private static final long serialVersionUID = 1L;
@Override
public boolean filter(
OQueryContextNativeSchema<ODocument> criteria) {
return criteria.field(CHAR_ID)
.eq(friend.getCharacterID().getID()).and()
.field(CHAR_ID_FRIEND)
.eq(friend.getFriendID().getID()).go();
};
};
}
@Override
protected ODocument update(ODocument document,
CharacterFriend object) throws SQLException {
document.delete();
return null;
}
@Override
protected ODocument insert(ODocument document,
CharacterFriend friend) throws SQLException {
return null;
}
}) > 0;
}
@Override
public boolean save(final CharacterFriendList friends) {
for (final CharacterFriend friend : friends) {
if (!save(friend))
return false;
}
return true;
}
@Override
public boolean delete(final CharacterFriendList friends) {
for (final CharacterFriend friend : friends) {
if (!delete(friend))
return false;
}
return true;
}
}

View File

@@ -0,0 +1,259 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.database.orientdb;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import com.google.inject.Inject;
import com.l2jserver.model.dao.CharacterDAO;
import com.l2jserver.model.dao.ChatMessageDAO;
import com.l2jserver.model.id.ChatMessageID;
import com.l2jserver.model.id.object.CharacterID;
import com.l2jserver.model.id.object.provider.CharacterIDProvider;
import com.l2jserver.model.id.provider.ChatMessageIDProvider;
import com.l2jserver.model.server.ChatMessage;
import com.l2jserver.service.database.AbstractOrientDatabaseService.CachedMapper;
import com.l2jserver.service.database.AbstractOrientDatabaseService.InsertUpdateQuery;
import com.l2jserver.service.database.AbstractOrientDatabaseService.Mapper;
import com.l2jserver.service.database.AbstractOrientDatabaseService.SelectListQuery;
import com.l2jserver.service.database.AbstractOrientDatabaseService.SelectSingleQuery;
import com.l2jserver.service.database.DatabaseService;
import com.l2jserver.service.game.chat.ChatMessageType;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.query.nativ.ONativeSynchQuery;
import com.orientechnologies.orient.core.query.nativ.OQueryContextNativeSchema;
import com.orientechnologies.orient.core.record.impl.ODocument;
/**
* {@link CharacterDAO} implementation for JDBC
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public abstract class OrientDBChatMessageDAO extends
AbstractOrientDBDAO<ChatMessage, ChatMessageID> implements
ChatMessageDAO {
/**
* The {@link ChatMessageID} factory
*/
private final ChatMessageIDProvider idFactory;
/**
* The {@link CharacterID} factory
*/
private final CharacterIDProvider charIdFactory;
/**
* Character table name
*/
public static final String CLASS_NAME = ChatMessage.class.getSimpleName();
// FIELDS
public static final String MESSAGE_ID = "message_id";
public static final String TYPE = "type";
public static final String CHANNEL_ID = "channel_id";
public static final String SENDER = "sender";
public static final String DATE = "date";
public static final String MESSAGE = "message";
/**
* @param database
* the database service
* @param idFactory
* the chat message id provider
* @param charIdFactory
* the character id provider
*/
@Inject
public OrientDBChatMessageDAO(DatabaseService database,
ChatMessageIDProvider idFactory,
final CharacterIDProvider charIdFactory) {
super(database);
this.idFactory = idFactory;
this.charIdFactory = charIdFactory;
}
/**
* The {@link Mapper} for {@link ChatMessageID}
*/
private final Mapper<ChatMessageID> idMapper = new Mapper<ChatMessageID>() {
@Override
public ChatMessageID map(ODocument document) throws SQLException {
return idFactory.resolveID((Integer) document.field(MESSAGE_ID));
}
};
/**
* The {@link Mapper} for {@link ChatMessage}
*/
private final Mapper<ChatMessage> mapper = new CachedMapper<ChatMessage, ChatMessageID>(
database, idMapper) {
@Override
protected ChatMessage map(ChatMessageID id, ODocument document)
throws SQLException {
final ChatMessage message = new ChatMessage();
message.setID(id);
message.setType(ChatMessageType.valueOf((String) document
.field(TYPE)));
switch (message.getType()) {
case SHOUT:
message.setTarget(charIdFactory.resolveID((Integer) document
.field(CHANNEL_ID)));
break;
default:
message.setChannelID((Integer) document.field(CHANNEL_ID));
break;
}
message.setSender(charIdFactory.resolveID((Integer) document
.field(SENDER)));
message.setDate((Date) document.field(DATE));
message.setMessage((String) document.field(MESSAGE));
return message;
}
};
@Override
public ChatMessage select(final ChatMessageID id) {
return database.query(new SelectSingleQuery<ChatMessage>() {
@Override
protected ONativeSynchQuery<ODocument, OQueryContextNativeSchema<ODocument>> createQuery(
ODatabaseDocumentTx database) {
return new ONativeSynchQuery<ODocument, OQueryContextNativeSchema<ODocument>>(
database, CLASS_NAME,
new OQueryContextNativeSchema<ODocument>()) {
private static final long serialVersionUID = 1L;
@Override
public boolean filter(
OQueryContextNativeSchema<ODocument> criteria) {
return criteria.field(MESSAGE_ID).eq(id.getID()).go();
};
};
}
@Override
protected Mapper<ChatMessage> mapper() {
return mapper;
}
});
}
@Override
public List<ChatMessageID> selectIDs() {
return database.query(new SelectListQuery<ChatMessageID>() {
@Override
protected ONativeSynchQuery<ODocument, OQueryContextNativeSchema<ODocument>> createQuery(
ODatabaseDocumentTx database) {
return new ONativeSynchQuery<ODocument, OQueryContextNativeSchema<ODocument>>(
database, CLASS_NAME,
new OQueryContextNativeSchema<ODocument>()) {
private static final long serialVersionUID = 1L;
@Override
public boolean filter(
OQueryContextNativeSchema<ODocument> criteria) {
return true;
};
};
}
@Override
protected Mapper<ChatMessageID> mapper() {
return idMapper;
}
});
}
@Override
public boolean insert(ChatMessage message) {
return database.query(new InsertUpdateQuery<ChatMessage>(message) {
@Override
protected ONativeSynchQuery<ODocument, OQueryContextNativeSchema<ODocument>> createQuery(
ODatabaseDocumentTx database, ChatMessage object) {
return null;
}
@Override
protected ODocument update(ODocument document, ChatMessage object)
throws SQLException {
return null;
}
@Override
protected ODocument insert(ODocument document, ChatMessage message)
throws SQLException {
document.field(TYPE, message.getType().name());
switch (message.getType()) {
case SHOUT:
document.field(CHANNEL_ID, message.getTarget().getID());
break;
default:
document.field(CHANNEL_ID, message.getChannelID());
break;
}
document.field(SENDER, message.getTarget().getID());
document.field(DATE, message.getDate());
document.field(MESSAGE, message.getMessage());
return document;
}
}) > 0;
}
@Override
public boolean update(ChatMessage message) {
// cannot update chat message logs
return false;
}
@Override
public boolean delete(ChatMessage message) {
return database.query(new InsertUpdateQuery<ChatMessage>(message) {
@Override
protected ONativeSynchQuery<ODocument, OQueryContextNativeSchema<ODocument>> createQuery(
ODatabaseDocumentTx database, final ChatMessage message) {
return new ONativeSynchQuery<ODocument, OQueryContextNativeSchema<ODocument>>(
database, CLASS_NAME,
new OQueryContextNativeSchema<ODocument>()) {
private static final long serialVersionUID = 1L;
@Override
public boolean filter(
OQueryContextNativeSchema<ODocument> criteria) {
return criteria.field(MESSAGE_ID).eq(message.getID())
.and().go();
};
};
}
@Override
protected ODocument update(ODocument document, ChatMessage object)
throws SQLException {
document.delete();
return null;
}
@Override
protected ODocument insert(ODocument document, ChatMessage object)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
}) > 0;
}
}