All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.beigesoft.ws.prc.ItmSpSv Maven / Gradle / Ivy

Go to download

It consists of double entry accounting and trading (web-store) business logic. It's based on previous beigesoft-accounting and beigesoft-webstore projects.

The newest version!
/*
BSD 2-Clause License

Copyright (c) 2019, Beigesoft™
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.beigesoft.ws.prc;

import java.util.Date;
import java.util.Map;
import java.util.HashMap;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.File;

import org.beigesoft.exc.ExcCode;
import org.beigesoft.mdl.IReqDt;
import org.beigesoft.rdb.IOrm;
import org.beigesoft.prc.IPrcEnt;
import org.beigesoft.prc.PrcEnfSv;
import org.beigesoft.ws.mdl.EItmSpTy;
import org.beigesoft.ws.mdlb.AItmSpf;
import org.beigesoft.ws.mdlb.AItmSpfId;

/**
 * 

Service that saves item specifics into DB.

* * @param entity type * @param entity ID type * @author Yury Demidenko */ public class ItmSpSv, ID extends AItmSpfId> implements IPrcEnt { /** *

ORM service.

**/ private IOrm orm; /** *

Upload directory relative to WEB-APP path * without start and end separator, e.g. "static/uploads".

**/ private String uplDir; /** *

Full WEB-APP path without end separator, * revealed from servlet context and used for upload files.

**/ private String appPth; /** *

Delegate.

**/ private PrcEnfSv prcEnfSv; /** *

Process that saves entity.

* @param pRvs request scoped vars, e.g. return this line's * owner(document) in "nextEntity" for farther processing * @param pRqDt Request Data * @param pEnt Entity to process * @return Entity processed for farther process or null * @throws Exception - an exception **/ @Override public final T process(final Map pRvs, final T pEnt, final IReqDt pRqDt) throws Exception { Map vs = new HashMap(); this.orm.refrEnt(pRvs, vs, pEnt.getSpec()); if (pRqDt.getParam("parFile") != null) { //file upload need: if (pEnt.getSpec().getTyp() == EItmSpTy.FILE_EMBEDDED) { return procFemb(pRvs, pEnt, pRqDt); } else { return this.prcEnfSv.process(pRvs, pEnt, pRqDt); } } else { if (pEnt.getSpec().getChSpTy() != null) { pEnt.setLng2(pEnt.getSpec().getChSpTy().getIid()); pEnt.setStr2(pEnt.getSpec().getChSpTy().getNme()); } if (pEnt.getIsNew()) { this.orm.insert(pRvs, vs, pEnt); pRvs.put("msgSuc", "insert_ok"); } else { this.orm.update(pRvs, vs, pEnt); pRvs.put("msgSuc", "update_ok"); } return pEnt; } } /** *

Process that saves entity with file embed.

* @param pRvs request scoped vars, e.g. return this line's * owner(document) in "nextEntity" for farther processing * @param pRqDt Request Data * @param pEnt Entity to process * @return Entity processed for farther process or null * @throws Exception - an exception **/ public final T procFemb(final Map pRvs, final T pEnt, final IReqDt pRqDt) throws Exception { Map vs = new HashMap(); OutputStream outs = null; InputStream ins = null; try { String fileUplNm = (String) pRqDt.getAttr("fileUplNm"); String filePath; if (pEnt.getStr1() == null) { //in base language: String ft = String.valueOf(new Date().getTime()); filePath = this.appPth + File.separator + this.uplDir + File.separator + ft + fileUplNm; pEnt.setStr1(this.uplDir + File.separator + ft + fileUplNm); pEnt.setStr2(filePath); } else { //i18n: String fiLng = pRqDt.getParam("fiLng"); if (pEnt.getStr3() == null || !pEnt.getStr3().contains(fiLng)) { throw new ExcCode(ExcCode.WRPR, "notset_language"); } int idhHtml = pEnt.getStr1().indexOf(".html"); String urlWithoutHtml = pEnt.getStr1().substring(0, idhHtml); filePath = this.appPth + File.separator + urlWithoutHtml + "_" + fiLng + ".html"; } ins = (InputStream) pRqDt.getAttr("fileUplIs"); outs = new BufferedOutputStream(new FileOutputStream(filePath)); byte[] data = new byte[1024]; int count; while ((count = ins.read(data)) != -1) { outs.write(data, 0, count); } outs.flush(); } finally { if (ins != null) { ins.close(); } if (outs != null) { outs.close(); } } if (pEnt.getIsNew()) { this.orm.insert(pRvs, vs, pEnt); pRvs.put("msgSuc", "insert_ok"); } else { this.orm.update(pRvs, vs, pEnt); pRvs.put("msgSuc", "update_ok"); } return pEnt; } //Simple getters and setters: /** *

Getter for orm.

* @return IOrm **/ public final IOrm getOrm() { return this.orm; } /** *

Setter for orm.

* @param pOrm reference **/ public final void setOrm(final IOrm pOrm) { this.orm = pOrm; } /** *

Getter for prcEnfSv.

* @return PrcEnfSv **/ public final PrcEnfSv getPrcEnfSv() { return this.prcEnfSv; } /** *

Setter for prcEnfSv.

* @param pPrcEnfSv reference **/ public final void setPrcEnfSv(final PrcEnfSv pPrcEnfSv) { this.prcEnfSv = pPrcEnfSv; } /** *

Getter for uplDir.

* @return String **/ public final String getUplDir() { return this.uplDir; } /** *

Setter for uplDir.

* @param pUplDir reference **/ public final void setUplDir(final String pUplDir) { this.uplDir = pUplDir; } /** *

Getter for appPth.

* @return String **/ public final String getAppPth() { return this.appPth; } /** *

Setter for appPth.

* @param pAppPth reference **/ public final void setAppPth(final String pAppPth) { this.appPth = pAppPth; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy