2019-11-23 21:16:03 +01:00
|
|
|
package org.mercury_im.messenger.data.mapping;
|
|
|
|
|
2019-12-02 00:37:35 +01:00
|
|
|
import org.mercury_im.messenger.data.enums.SubscriptionDirection;
|
2019-11-23 21:16:03 +01:00
|
|
|
import org.mercury_im.messenger.data.model.PeerModel;
|
|
|
|
import org.mercury_im.messenger.entity.contact.IPeer;
|
|
|
|
import org.mercury_im.messenger.entity.contact.Peer;
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
|
|
public class PeerMapping implements Mapping<Peer, PeerModel> {
|
|
|
|
|
2019-12-01 19:56:13 +01:00
|
|
|
private final AccountMapping accountMapping;
|
|
|
|
|
2019-11-23 21:16:03 +01:00
|
|
|
@Inject
|
2019-12-01 19:56:13 +01:00
|
|
|
public PeerMapping(AccountMapping accountMapping) {
|
|
|
|
this.accountMapping = accountMapping;
|
|
|
|
}
|
2019-11-23 21:16:03 +01:00
|
|
|
|
|
|
|
@Override
|
2019-11-25 15:17:49 +01:00
|
|
|
public PeerModel entityToModel(Peer entity, PeerModel model) {
|
2019-12-01 19:56:13 +01:00
|
|
|
if (entity == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (model == null) {
|
|
|
|
model = new PeerModel();
|
|
|
|
}
|
2019-11-25 15:17:49 +01:00
|
|
|
model.setAccount(accountMapping.entityToModel(entity.getAccount(), model.getAccount()));
|
2019-11-23 21:16:03 +01:00
|
|
|
model.setAddress(entity.getAddress());
|
2019-12-02 00:37:35 +01:00
|
|
|
model.setName(entity.getName());
|
|
|
|
|
|
|
|
// TODO: sub direction
|
|
|
|
|
2019-11-23 21:16:03 +01:00
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-25 15:17:49 +01:00
|
|
|
public Peer modelToEntity(PeerModel model, Peer entity) {
|
2019-12-01 19:56:13 +01:00
|
|
|
if (model == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-12-01 21:16:01 +01:00
|
|
|
if (entity == null) {
|
|
|
|
entity = new IPeer();
|
|
|
|
}
|
2019-11-25 15:17:49 +01:00
|
|
|
entity.setAccount(accountMapping.modelToEntity(model.getAccount(), entity.getAccount()));
|
|
|
|
entity.setAddress(model.getAddress());
|
|
|
|
entity.setId(model.getId());
|
2019-12-02 00:37:35 +01:00
|
|
|
|
|
|
|
// TODO: Sub direction
|
2019-11-25 15:17:49 +01:00
|
|
|
return entity;
|
2019-11-23 21:16:03 +01:00
|
|
|
}
|
2019-12-02 00:37:35 +01:00
|
|
|
|
|
|
|
private void setSubscriptionDirectionInModel(PeerModel model, Peer entity) {
|
|
|
|
switch (entity.getSubscriptionMode()) {
|
|
|
|
case NONE:
|
|
|
|
model.setSubscriptionDirection(SubscriptionDirection.none);
|
|
|
|
model.setSubscriptionPreApproved(false);
|
|
|
|
model.setSubscriptionPending(false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NONE_PREAPPROVED:
|
|
|
|
model.setSubscriptionDirection(SubscriptionDirection.none);
|
|
|
|
model.setSubscriptionPreApproved(true);
|
|
|
|
model.setSubscriptionPending(false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TO_THEM_PENDING:
|
|
|
|
model.setSubscriptionDirection(SubscriptionDirection.to);
|
|
|
|
model.setSubscriptionPending(true);
|
|
|
|
model.setSubscriptionPreApproved(false);
|
|
|
|
break;
|
|
|
|
case TO_THEM_PENDING_PREAPPROVED:
|
|
|
|
model.setSubscriptionDirection(SubscriptionDirection.to);
|
|
|
|
model.setSubscriptionPending(true);
|
|
|
|
model.setSubscriptionPreApproved(true);
|
|
|
|
break;
|
|
|
|
case TO_THEM_ACCEPTED:
|
|
|
|
model.setSubscriptionDirection(SubscriptionDirection.to);
|
|
|
|
model.setSubscriptionPending(false);
|
|
|
|
model.setSubscriptionPreApproved(false);
|
|
|
|
break;
|
|
|
|
case TO_THEM_ACCEPTED_PREAPPROVED:
|
|
|
|
model.setSubscriptionDirection(SubscriptionDirection.to);
|
|
|
|
model.setSubscriptionPending(false);
|
|
|
|
model.setSubscriptionPreApproved(true);
|
|
|
|
break;
|
|
|
|
case FROM_THEM_PENDING:
|
|
|
|
model.setSubscriptionDirection(SubscriptionDirection.from);
|
|
|
|
model.setSubscriptionPending(true);
|
|
|
|
model.setSubscriptionPreApproved(false);
|
|
|
|
break;
|
|
|
|
case FROM_THEM_ACCEPTED:
|
|
|
|
model.setSubscriptionDirection(SubscriptionDirection.from);
|
|
|
|
model.setSubscriptionPending(false);
|
|
|
|
model.setSubscriptionPreApproved(false);
|
|
|
|
break;
|
|
|
|
case BIDIRECTIONAL:
|
|
|
|
model.setSubscriptionDirection(SubscriptionDirection.both);
|
|
|
|
model.setSubscriptionPending(false);
|
|
|
|
model.setSubscriptionPreApproved(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setSubscriptionDirectionInEntity(Peer entity, PeerModel model) {
|
|
|
|
|
|
|
|
switch (model.getSubscriptionDirection()) {
|
|
|
|
case none:
|
|
|
|
break;
|
|
|
|
case to:
|
|
|
|
break;
|
|
|
|
case from:
|
|
|
|
break;
|
|
|
|
case both:
|
|
|
|
break;
|
|
|
|
case remove:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-11-23 21:16:03 +01:00
|
|
|
}
|