package edu.ualberta.med.biobank.common.wrappers;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import edu.ualberta.med.biobank.common.exception.BiobankCheckException;
import edu.ualberta.med.biobank.common.exception.BiobankException;
import edu.ualberta.med.biobank.common.peer.ShipmentInfoPeer;
import edu.ualberta.med.biobank.common.peer.ShippingMethodPeer;
import edu.ualberta.med.biobank.common.wrappers.WrapperTransaction.TaskList;
import edu.ualberta.med.biobank.common.wrappers.base.ShippingMethodBaseWrapper;
import edu.ualberta.med.biobank.model.ShipmentInfo;
import edu.ualberta.med.biobank.model.ShippingMethod;
import gov.nih.nci.system.applicationservice.ApplicationException;
import gov.nih.nci.system.applicationservice.WritableApplicationService;
import gov.nih.nci.system.query.hibernate.HQLCriteria;
public class ShippingMethodWrapper extends ShippingMethodBaseWrapper {
public static final String DROP_OFF_NAME = "Drop-off"; //$NON-NLS-1$
public static final String PICK_UP_NAME = "Pick-up"; //$NON-NLS-1$
public ShippingMethodWrapper(WritableApplicationService appService) {
super(appService);
}
public ShippingMethodWrapper(WritableApplicationService appService,
ShippingMethod sc) {
super(appService, sc);
}
@Override
public boolean equals(Object object) {
if (object instanceof ShippingMethodWrapper)
return ((ShippingMethodWrapper) object).getName().equals(
this.getName());
return false;
}
@Override
public int compareTo(ModelWrapper<ShippingMethod> o) {
if (o instanceof ShippingMethodWrapper) {
return getName().compareTo(o.getWrappedObject().getName());
}
return 0;
}
public static List<ShippingMethodWrapper> getShippingMethods(
WritableApplicationService appService) throws ApplicationException {
List<ShippingMethod> objects = appService.query(DetachedCriteria
.forClass(ShippingMethod.class));
List<ShippingMethodWrapper> wrappers =
new ArrayList<ShippingMethodWrapper>();
for (ShippingMethod sm : objects) {
wrappers.add(new ShippingMethodWrapper(appService, sm));
}
return wrappers;
}
@Override
public String toString() {
return getName();
}
private static final String IS_USED_HQL = "select count(si) from " //$NON-NLS-1$
+ ShipmentInfo.class.getName() + " as si where si." //$NON-NLS-1$
+ ShipmentInfoPeer.SHIPPING_METHOD.getName() + "=?"; //$NON-NLS-1$
public boolean isUsed() throws ApplicationException, BiobankException {
if (isNew())
return false;
HQLCriteria c = new HQLCriteria(IS_USED_HQL,
Arrays.asList(new Object[] { wrappedObject }));
return getCountResult(appService, c) > 0;
}
// TODO: is this needed anymore?
@Deprecated
public static void persistShippingMethods(
List<ShippingMethodWrapper> addedOrModifiedTypes,
List<ShippingMethodWrapper> typesToDelete)
throws BiobankCheckException, Exception {
if (addedOrModifiedTypes != null) {
for (ShippingMethodWrapper ss : addedOrModifiedTypes) {
ss.persist();
}
}
if (typesToDelete != null) {
for (ShippingMethodWrapper ss : typesToDelete) {
ss.delete();
}
}
}
public boolean needDate() {
String name = getName();
return name != null && !name.equals(PICK_UP_NAME)
&& !name.equals(DROP_OFF_NAME);
}
@Deprecated
@Override
protected void addPersistTasks(TaskList tasks) {
tasks.add(check().notNull(ShippingMethodPeer.NAME));
tasks.add(check().unique(ShippingMethodPeer.NAME));
super.addPersistTasks(tasks);
}
@Deprecated
@Override
protected void addDeleteTasks(TaskList tasks) {
tasks.add(check().notUsedBy(ShipmentInfo.class,
ShipmentInfoPeer.SHIPPING_METHOD));
super.addDeleteTasks(tasks);
}
}