/*
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.module.openhmis.inventory.api.model;

import org.openmrs.User;
import org.openmrs.module.openhmis.commons.api.entity.model.BaseCustomizableInstanceObject;

import java.util.Date;
import java.util.Set;
import java.util.TreeSet;

public class StockRoomTransaction extends BaseCustomizableInstanceObject<StockRoomTransactionAttribute>
	implements Comparable<StockRoomTransaction> {
	public static final long serialVersionUID = 0L;

	private Integer stockRoomTransferId;
	private String transactionNumber;
	private StockRoomTransactionType transactionType;
	private StockRoomTransactionStatus status;
	private StockRoom source;
	private StockRoom destination;
	private Set<StockRoomTransactionItem> items;
	private Boolean isImportTransaction;
	private User creator;
	private Date dateCreated;

	@Override
	public Integer getId() {
		return stockRoomTransferId;
	}

	@Override
	public void setId(Integer id) {
		stockRoomTransferId = id;
	}

	public String getTransactionNumber() {
		return transactionNumber;
	}

	public void setTransactionNumber(String transactionNumber) {
		this.transactionNumber = transactionNumber;
	}

	public StockRoomTransactionType getTransactionType() {
		return transactionType;
	}

	public void setTransactionType(StockRoomTransactionType transactionType) {
		this.transactionType = transactionType;
	}

	public StockRoomTransactionStatus getStatus() {
		return status;
	}

	public void setStatus(StockRoomTransactionStatus status) {
		this.status = status;
	}

	public StockRoom getSource() {
		return source;
	}

	public void setSource(StockRoom newSource) {
		if (this.source == newSource) {
			return;
		}

		// If source exists then remove this transaction from it
		if (this.source != null && this.source != this.destination) {
			this.source.removeTransaction(this);
		}

		// Update the source
		this.source = newSource;

		// If the new source is not null then add this transaction to it
		if (this.source != null) {
			this.source.addTransaction(this);
		}
	}

	public StockRoom getDestination() {
		return destination;
	}

	public void setDestination(StockRoom newDestination) {
		if (this.destination == newDestination) {
			return;
		}

		// If destination exists then remove this transaction from it
		if (this.destination != null && this.destination != this.source) {
			this.destination.removeTransaction(this);
		}

		// Update the destination
		this.destination = newDestination;

		// If the new destination is not null then add this transaction to it
		if (this.destination != null) {
			this.destination.addTransaction(this);
		}
	}

	public Set<StockRoomTransactionItem> getItems() {
		return items;
	}

	public void setItems(Set<StockRoomTransactionItem> items) {
		this.items = items;
	}

	public void addItem(StockRoomTransactionItem item) {
		if (item != null) {
			if (items == null) {
				items = new TreeSet<StockRoomTransactionItem>();
			}

			item.setTransaction(this);
			items.add(item);
		}
	}

	public void removeItem(StockRoomTransactionItem item) {
		if (item != null) {
			if (items == null) {
				return;
			}

			item.setTransaction(null);
			items.remove(item);
		}
	}

	public Boolean isImportTransaction() {
		return getImportTransaction();
	}

	public Boolean getImportTransaction() {
		return isImportTransaction;
	}

	public void setImportTransaction(Boolean importTransaction) {
		isImportTransaction = importTransaction;
	}

	public User getCreator() {
		return creator;
	}

	public void setCreator(User creator) {
		this.creator = creator;
	}

	public Date getDateCreated() {
		return dateCreated;
	}

	public void setDateCreated(Date dateCreated) {
		this.dateCreated = dateCreated;
	}

	@Override
	public int compareTo(StockRoomTransaction o) {
		// The default sorting uses the transaction creation date
		if (this.getDateCreated() == null || this.getDateCreated() == null) {
			return 0;
		}
		
		return this.getDateCreated().compareTo(o.getDateCreated());
	}
}

@@ -615,20 +615,93 @@
 openmrs.
-User
+Attributable;%0Aimport org.openmrs.User;%0Aimport org.openmrs.api.context.Context
 ;%0Aimport
@@ -785,16 +785,99 @@
 eObject;
+%0Aimport org.openmrs.module.openhmis.inventory.api.IStockRoomTransactionDataService;
 %0A%0Aimport
@@ -893,16 +893,39 @@
 l.Date;%0A
+import java.util.List;%0A
 import j
@@ -1069,16 +1069,16 @@
 ribute%3E%0A
-
 %09impleme
@@ -1113,16 +1113,52 @@
 saction%3E
+, Attributable%3CStockRoomTransaction%3E
  %7B%0A%09publ
@@ -4712,11 +4712,526 @@
 d());%0A%09%7D
+%0A%0A%09@Override%0A%09public StockRoomTransaction hydrate(String s) %7B%0A%09%09return Context.getService(IStockRoomTransactionDataService.class).getById(Integer.parseInt(s));%0A%09%7D%0A%0A%09@Override%0A%09public String serialize() %7B%0A%09%09if (getId() != null)%0A%09%09%09return %22%22 + getId();%0A%09%09return %22%22;%0A%09%7D%0A%0A%09@Override%0A%09public List%3CStockRoomTransaction%3E getPossibleValues() %7B return null; %7D%0A%0A%09@Override%0A%09public List%3CStockRoomTransaction%3E findPossibleValues(String searchText) %7B return null; %7D%0A%0A%09@Override%0A%09public String getDisplayString() %7B return null; %7D
 %0A%7D%0A

Make StockRoomTransaction implement Attributable