/*
 * Isomorphic SmartGWT web presentation layer
 * Copyright 2000 and beyond Isomorphic Software, Inc.
 *
 * OWNERSHIP NOTICE
 * Isomorphic Software owns and reserves all rights not expressly granted in this source code,
 * including all intellectual property rights to the structure, sequence, and format of this code
 * and to all designs, interfaces, algorithms, schema, protocols, and inventions expressed herein.
 *
 *  If you have any questions, please email <sourcecode@isomorphic.com>.
 *
 *  This entire comment must accompany any portion of Isomorphic Software source code that is
 *  copied or moved from this file.
 */

package com.smartgwt.sample.showcase.server.customDataSource;

//----------------------------------------------------------------------
// Custom DataSource example
//
// This class shows how to easily implement a completely customized
// DataSource that simply plugs into the SmartGWT Server framework
//----------------------------------------------------------------------

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.isomorphic.datasource.BasicDataSource;
import com.isomorphic.datasource.DSRequest;
import com.isomorphic.datasource.DSResponse;
import com.isomorphic.util.DataTools;

public class UserDataSource extends BasicDataSource {

    private static List data = new ArrayList();
    private static int nextId;

    // Hard-coded data store
    static {
        String[] userName = { "Charles Madigen", "Ralph Brogan", "Grigori Ognev", "Tamara Kane",
                              "Betsy Rosenbaum", "Gene Porter", "Prya Sambhus", "Ren Xian", 
"Person 01", "Person 02", "Person 03", "Person 04", "Person 05", "Person 06", "Person 07", "Person 08", "Person 09", "Person 10", "Person 11", "Person 12"  };
        String[] jobTitle = { "Chief Operating Officer", "Manager Systems", "Technician",
                              "Manager Site Services", "Secretary", "Manager Purchasing",
                              "Line Worker", "Mobile Equipment Operator", 
"Title 01", "Title 02", "Title 03", "Title 04", "Title 05", "Title 06", "Title 07", "Title 08", "Title 09", "Title 10", "Title 11", "Title 12"};
        String[] email    = { "charles.madigen", "ralph.brogan", "grigori.ognev", "tamara.kane",
                              "elizabeth.rosenbaum", "gene.porter", "prya.sambhus", "ren.xian",
"email 01","email 02","email 03","email 04","email 05","email 06","email 07","email 08","email 09","email 10","email 11", "email 12" };
        String[] type     = { "full time", "contract", "part time", "full time", "part time",
                              "contract", "part time", "full time",
"type 01", "type 02", "type 03", "type 04", "type 05", "type 06", "type 07", "type 08", "type 09", "type 10", "type 11", "type 12"							  };
        float[] salary      = { 20395, 18076, 12202, 21227, 11632, 17702, 12985, 16402,
10001,10002,10003,10004,10005,10006,10007,10008,10009,10010,10011,10012};


        for (int i = 0; i < userName.length; i++) {
            Map map = new HashMap();
            map.put("employeeId", new Integer(i + 1));
            map.put("userName", userName[i]);
            map.put("job", jobTitle[i]);
            map.put("email", email[i] + "@server.com");
            map.put("employeeType", type[i]);
            map.put("salary", new Float(salary[i]));

            map.put("userName1", userName[i]);
            map.put("job1", jobTitle[i]);
            map.put("email1", email[i] + "@server.com");
            map.put("employeeType1", type[i]);
            map.put("salary1", new Float(salary[i]));

            map.put("userName2", userName[i]);
            map.put("job2", jobTitle[i]);
            map.put("email2", email[i] + "@server.com");
            map.put("employeeType2", type[i]);
            map.put("salary2", new Float(salary[i]));

            map.put("userName3", userName[i]);
            map.put("job3", jobTitle[i]);
            map.put("email3", email[i] + "@server.com");
            map.put("employeeType3", type[i]);
            map.put("salary3", new Float(salary[i]));

            data.add(map);
        }
        nextId = data.size() + 1;
    }

    // Override all four CRUD operations - create, retrieve, update and delete
    // (add, fetch, update and remove in SmartClient terminology).

    // Note that the parameters sent by the client arrive here already converted to Java Maps
    // by the SmartClient Server - with SmartClient Pro and Enterprise, there's no need to worry
    // about conversion to and from XML or JSON even in a custom DS implementation

    public DSResponse executeAdd(DSRequest req) throws Exception {
        Map map = req.getValues();
        map.put("employeeId", new Integer(nextId++));
        data.add(map);
        DSResponse resp = new DSResponse();
        resp.setData(req.getValues());
        resp.setStatus(0);
        return resp;
    }

    // There isn't enough data in this hard-coded example to make it worthwhile implementing
    // paging; however, we could easily do so by using req.getStartRow() and req.getEndRow() to
    // return just those records of the complete filtered list.
    public DSResponse executeFetch(DSRequest req) throws Exception {
        List filteredData = null;
        if (req.getCriteria() != null) {
            String partialName = (String)req.getCriteria().get("userName");
            if (partialName != null) {
                filteredData = new ArrayList();
                for (Iterator i = data.iterator(); i.hasNext(); ) {
                    Map map = (Map)i.next();
                    String userName = (String)map.get("userName");
                    if (userName != null && userName.contains(partialName)) {
                        filteredData.add(map);
                    }
                }
            }
        }
        DSResponse resp = new DSResponse();
        resp.setData(filteredData == null ? data : filteredData);
        resp.setStatus(0);
        return resp;
    }

    public DSResponse executeRemove(DSRequest req) throws Exception {
        Long work = (Long)req.getValues().get("employeeId");
        if (work != null) {
            Integer key = new Integer(work.intValue());
            for (Iterator i = data.iterator(); i.hasNext(); ) {
                Map map = (Map)i.next();
                if (key.equals(map.get("employeeId"))) {
                    i.remove();
                    break;
                }
            }
        }
        DSResponse resp = new DSResponse();
        resp.setData(req.getOldValues());
        resp.setStatus(0);
        return resp;
    }

    public DSResponse executeUpdate(DSRequest req) throws Exception {
        Map map = req.getOldValues();
        Long work = (Long)req.getValues().get("employeeId");
        if (work != null) {
            Integer key = new Integer(work.intValue());
            for (int i = 0; i < data.size(); i++) {
                map = (Map)data.get(i);
                if (key.equals(map.get("employeeId"))) {
                    DataTools.mapMerge(req.getOldValues(), map);
                    DataTools.mapMerge(req.getValues(), map);
                    break;
                }
            }
        }
        DSResponse resp = new DSResponse();
        resp.setData(map);
        resp.setStatus(0);
        return resp;
    }
}