/*
 *  File services class
 *
 *  2004 - 2011 Frantisek Zboril jr.
 *  Faculty of Information Technology
 *  zborilf@fit.vutbr.cz
 *  http://www.fit.vutbr.cz/~zborilf
 *
 * (C) Brno University of Technology
 * Freeware licence VUT v Brně
 * https://wis.fit.vutbr.cz/FIT/db/dir.php/dr/VZ2007/externiInformace/softwareNaWebu/Priloha2_rozh23.pdf

 */

package jTMass.core.jTMassPlatform.jTServices;

import jTMass.TMass;
import jTMass.core.jALLL.jTObjects.TFail;
import jTMass.core.jALLL.jTObjects.TList;
import jTMass.core.jALLL.jTObjects.TObject;
import jTMass.core.jTMassPlatform.TPlatform;
import java.io.File;
import java.io.FileWriter;

/**
 *
 * @author zborilf
 */
public class TSFileServices extends TASSynchronousService{

    @Override
    public TObject processService(TList parameters, String clientName, String serviceName){

        /*
         * r - reset all
         * c - create file
         * w - write to file
         */

        super.processService(parameters,clientName,serviceName);

        String operator=getIth(1,parameters).getContent();
        String fileName=getIth(2,parameters).getContent();
        if(operator.contentEquals("r")){
            return(null);
        }

        try{
            File mFile=new File(TMass.getModelPath()+File.separator+"platforms"+File.separator+
                    PPlatform.getElementName()+File.separator+clientName+File.separator+fileName+".psf");
            File mParent=mFile.getParentFile();
            if(operator.contentEquals("c")||operator.contentEquals("w")){
                mParent.mkdirs();
                mFile.createNewFile();
            }
            if(operator.contentEquals("wl")){
                String value=getIth(3,parameters).getContent();
                FileWriter writer=new FileWriter(mFile.getAbsoluteFile(),true);
                writer.write(value+"\n");
                writer.close();
            }
            return(null);
        }catch(Exception e){
            TMass._Terminal.reportError("Platform files service: Invalid parameter(s)");
            return(new TFail());
        }
    }

    public TSFileServices(TPlatform platform){
        super(platform);
    }
}
