package sgcib.nemo.nails.server;

import java.util.*;
import com.isomorphic.log.*;
import com.isomorphic.util.*;
import com.isomorphic.datasource.*;


public class TradeDMI 
{
    Logger log = new Logger("nails");

    public DSResponse fetch(DSRequest dsRequest)throws Exception
    {
        log.info("procesing DMI fetch operation");

        // Fetch a List of matching SupplyItem Beans from some pre-existing Java object model
        // provided by you, represented by "SupplyItemStore" in this example
        List matchingItems =
            TradeStore.findMatchingItems((String)dsRequest.getFieldValue("tradeTrackingId"));

        // this implementation shows data paging (returning only ranges of requested records)
        long startRow = dsRequest.getStartRow();
        long endRow = dsRequest.getEndRow();

        long totalRows = matchingItems.size();
        DSResponse dsResponse = new DSResponse();
        dsResponse.setTotalRows(totalRows);
        dsResponse.setStartRow(startRow);

        endRow = Math.min(endRow, totalRows);
        dsResponse.setEndRow(endRow);

        // trim the data to the requested range of records.  In a real application, the startRow
        // and endRow would be passed to the ORM layer or to SQL for maximum efficiency.
        List results;
        if (totalRows > 0) 
        {
            results = matchingItems.subList((int)dsResponse.getStartRow(),
                                            (int)dsResponse.getEndRow());
        }
        else 
        {
            results = matchingItems;
        }

        // just return the List of matching beans
        dsResponse.setData(results);

        return dsResponse;
    }

    // Declared parameters of the method are automatically provided.  For example, if your
    // method declares a parameter of type HttpServletRequest, the current HttpServletRequest
    // will be passed.
    // If you declare a parameter that is a Java Bean, SmartClient will create an instance of
    // the bean and apply the DSRequest data to the bean via Java reflection, matching
    // DataSource fields to Java setter methods.  For example, for the DataSource field
    // "itemName", SupplyItem.setItemName() is called.
    public Trade add(Trade record)
        throws Exception
    {
        log.info("procesing DMI add operation");

        // most ORM systems can store a new Object with a single call like this.
        TradeStore.storeItem(record);

        // return the record-as-saved so SmartClient can update client-side caches.  This
        // pattern ensures client-side components receive server-generated fields (such as an
        // auto-generated primaryKey value).  SmartClient can then update caches in place
        // instead of re-fetching data.
        return record;
    }

    // The "update" method declares a parameter of type "Map" and so receives DSRequest.data as
    // a Java Map.  For an "update" operation, the data includes updated fields and always
    // includes the field declared as primaryKey:true.
    public Trade update(Map record)
        throws Exception
    {
        log.info("procesing DMI update operation");

        // look up the existing data by primary key
        Trade existingRecord = TradeStore.getItemByID((String)record.get("ENTITY_TRACKING_ID"));

        // DataTools.setProperties() is a SmartClient API that applies a Map of properties to a
        // Bean via reflection, matching Map keys to setter methods on the Bean.
        DataTools.setProperties(record, existingRecord);

        // store it, and return the record-as-saved so SmartClient can update client-side caches.
        TradeStore.storeItem(existingRecord);
        return existingRecord;
    }


    public Trade remove(Trade record)
        throws Exception
    {
        log.info("procesing DMI remove operation");

        // on a removal, just return
        return TradeStore.removeItem(record.getENTITY_TRACKING_ID());
    }
}
