package voxo.server.actions;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.SQLException;
import voxo.common.entities.Contact;
import voxo.common.entities.User;
import voxo.common.enums.EnumPacket;
import voxo.common.packets.ClientRequestPacket;
import voxo.server.Messages;
import voxo.server.exceptions.RequestException;
import voxo.server.managers.ContactManager;
import voxo.server.managers.UserManager;
public class ContactRemoveAction {
public ContactRemoveAction(InetAddress ip, ClientRequestPacket o) throws UnknownHostException, IOException, SQLException, RequestException {
// Get the clients affected and the contact entry
User u = UserManager.searchUserAbsolute(new User(null, o.getUsername(), null, null, ip.getHostAddress(), true, null)).get(0);
User cu = UserManager.searchUserAbsolute(new User(null, o.getContactName(), null, null, null, null, null)).get(0);
if (u == null) {
throw new RequestException(Messages.getString("ContactRemoveAction.ERR_UserNotFound")); //$NON-NLS-1$
}
if (cu == null) {
throw new RequestException(Messages.getString("ContactRemoveAction.ERR_ContactUserNotFound")); //$NON-NLS-1$
}
Contact c = ContactManager.searchContactAbsolute(new Contact(null, cu.getUserId(), u.getUserId(), null)).get(0);
if (c == null) {
throw new RequestException(Messages.getString("ContactRemoveAction.ERR_ContactNotFound")); //$NON-NLS-1$
}
// Delete contact
ContactManager.deleteContact(c);
// Notify users
new SendContactListAction(u);
new SendMessageAction(String.format(Messages.getString("ContactRemoveAction.ResponseSelf"), cu.getUsername()), cu, EnumPacket.SERVER_Notice); //$NON-NLS-1$
new SendContactListAction(cu);
new SendMessageAction(String.format(Messages.getString("ContactRemoveAction.ResponseTarget"), u.getUsername()), cu, EnumPacket.SERVER_Notice); //$NON-NLS-1$
}
}