package voxo.server.actions;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.SQLException;
import java.util.ArrayList;
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 ContactAcceptAction {
public ContactAcceptAction(InetAddress ip, ClientRequestPacket o) throws UnknownHostException, IOException, SQLException, RequestException {
// Get the clients affected
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("ContactAcceptAction.ERR_UserNotFound")); //$NON-NLS-1$
}
if (cu == null) {
throw new RequestException(Messages.getString("ContactAcceptAction.ERR_ContactUserNotFound")); //$NON-NLS-1$
}
// Get existing contact
Contact c = new Contact(null, cu.getUserId(), u.getUserId(), null);
ArrayList<Contact> alc = ContactManager.searchContactAbsolute(c);
if (alc.size() != 1) {
throw new RequestException(Messages.getString("ContactAcceptAction.ERR_AlreadyContact")); //$NON-NLS-1$
}
// Update contact status
Contact cAccept = alc.get(0);
cAccept.setAccepted(true);
ContactManager.updateContact(cAccept);
// Notify users
new SendContactListAction(u);
new SendMessageAction(cu.getUsername() + Messages.getString("ContactAcceptAction.ResponseSelf"), u, EnumPacket.SERVER_Notice); //$NON-NLS-1$
new SendContactListAction(cu);
new SendMessageAction(String.format(Messages.getString("ContactAcceptAction.ReponseTarget"), u.getUsername()), cu, EnumPacket.SERVER_Notice); //$NON-NLS-1$
}
}