/*
 * MgtNetRouterServiceImpl.java
 *
 * Created on July 20, 2009, 10:55 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package com.portal.server.mgtNet;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.magti.businesslayer.ejb3entity.oracle.srvprov.mgtnet.MgtNetRouter;
import com.portal.client.beans.mgtnet.GWTMgtNetRouter;
import com.portal.client.beans.user.GWTUserContext;
import com.portal.client.common.components.ds.FetchResult;
import com.portal.client.exception.PortalException;
import com.portal.client.mgtNet.MgtNetService;
import com.portal.server.common.config.ConfigReader;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;

/**
 *
 * @author paatal
 */
public class MgtNetServiceImpl extends RemoteServiceServlet implements
        MgtNetService {

    public FetchResult<GWTMgtNetRouter> getMgtNetRouters(int startRow, int endRow, GWTUserContext context) throws PortalException {
        try {
            FetchResult<GWTMgtNetRouter> ret = new FetchResult<GWTMgtNetRouter>();
            long time = System.currentTimeMillis();
            ArrayList<GWTMgtNetRouter> list = new ArrayList<GWTMgtNetRouter>();
            ArrayList<MgtNetRouter> routers = ConfigReader.getInstance().getMgtNetManFascade().getMgtNetRouters();
            int i = 0;
            if (routers != null && !routers.isEmpty()) {
                for (i = startRow; i < endRow && i < routers.size(); i++) {
                    MgtNetRouter mgtNetRouter = routers.get(i);
                    GWTMgtNetRouter gWTMgtNetRouter = new GWTMgtNetRouter();
                    gWTMgtNetRouter.setId(mgtNetRouter.getId());
                    gWTMgtNetRouter.setIpAddress(mgtNetRouter.getIpAddress());
                    gWTMgtNetRouter.setName(mgtNetRouter.getName());
                    gWTMgtNetRouter.setRegDate(mgtNetRouter.getRegDate() == null ? null : new Date(mgtNetRouter.getRegDate().getTime()));
                    gWTMgtNetRouter.setUpdDate(mgtNetRouter.getUpdDate() == null ? null : new Date(mgtNetRouter.getUpdDate().getTime()));
                    list.add(gWTMgtNetRouter);
                }
            }
            time = System.currentTimeMillis() - time;
            ret.setFetchedList(list);
            ret.setStartRow(startRow);
            ret.setEndRow(i);
            ret.setTotalRows(list.size());
            System.out.println(list.size() + " Magti-Net Routers Loaded In :" + time + " Millisecond");
            return ret;
        } catch (Exception e) {
            if (e instanceof PortalException) {
                throw (PortalException) e;
            }
            throw new PortalException(e.getMessage());
        }
    }

    public GWTMgtNetRouter addMgtNetRouters(GWTMgtNetRouter gWTMgtNetRouter, GWTUserContext context) throws PortalException {
        try {
            long time = System.currentTimeMillis();
            MgtNetRouter mgtNetRouter = new MgtNetRouter();
            mgtNetRouter.setIpAddress(gWTMgtNetRouter.getIpAddress());
            mgtNetRouter.setName(gWTMgtNetRouter.getName());
            mgtNetRouter.setRegDate(gWTMgtNetRouter.getRegDate() == null ? new Timestamp(time) : new Timestamp(gWTMgtNetRouter.getRegDate().getTime()));
            mgtNetRouter.setUpdDate(new Timestamp(time));
            mgtNetRouter = ConfigReader.getInstance().getMgtNetManFascade().addMgtNetRouter(mgtNetRouter);
            gWTMgtNetRouter.setId(mgtNetRouter.getId());
            gWTMgtNetRouter.setIpAddress(mgtNetRouter.getIpAddress());
            gWTMgtNetRouter.setName(mgtNetRouter.getName());
            gWTMgtNetRouter.setRegDate(mgtNetRouter.getRegDate() == null ? new Timestamp(time) : new Timestamp(mgtNetRouter.getRegDate().getTime()));
            gWTMgtNetRouter.setUpdDate(new Timestamp(time));
            time = System.currentTimeMillis() - time;
            System.out.println("Adding Magti-Net Router Time : " + time);
            return gWTMgtNetRouter;
        } catch (Exception e) {
            if (e instanceof PortalException) {
                throw (PortalException) e;
            }
            throw new PortalException(e.getMessage());
        }
    }

    public GWTMgtNetRouter editMgtNetRouters(GWTMgtNetRouter gWTMgtNetRouter, GWTUserContext context) throws PortalException {
        try {
            long time = System.currentTimeMillis();
            MgtNetRouter mgtNetRouter = new MgtNetRouter();
            mgtNetRouter.setId(gWTMgtNetRouter.getId());
            mgtNetRouter.setIpAddress(gWTMgtNetRouter.getIpAddress());
            mgtNetRouter.setName(gWTMgtNetRouter.getName());
            mgtNetRouter.setRegDate(gWTMgtNetRouter.getRegDate() == null ? new Timestamp(time) : new Timestamp(gWTMgtNetRouter.getRegDate().getTime()));
            mgtNetRouter.setUpdDate(new Timestamp(time));
            mgtNetRouter = ConfigReader.getInstance().getMgtNetManFascade().editMgtNetRouter(mgtNetRouter);
            gWTMgtNetRouter.setId(mgtNetRouter.getId());
            gWTMgtNetRouter.setIpAddress(mgtNetRouter.getIpAddress());
            gWTMgtNetRouter.setName(mgtNetRouter.getName());
            gWTMgtNetRouter.setRegDate(mgtNetRouter.getRegDate() == null ? new Timestamp(time) : new Timestamp(mgtNetRouter.getRegDate().getTime()));
            gWTMgtNetRouter.setUpdDate(new Timestamp(time));
            time = System.currentTimeMillis() - time;
            System.out.println("Edit Magti-Net Router Time : " + time);
            return gWTMgtNetRouter;
        } catch (Exception e) {
            if (e instanceof PortalException) {
                throw (PortalException) e;
            }
            throw new PortalException(e.getMessage());
        }
    }

    public void removeMgtNetRouters(GWTMgtNetRouter gWTMgtNetRouter, GWTUserContext context) throws PortalException {
        try {
        } catch (Exception e) {
            if (e instanceof PortalException) {
                throw (PortalException) e;
            }
            throw new PortalException(e.getMessage());
        }
    }
}
