1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
| import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import com.businessobjects.rebean.wi.BinaryView;
import com.businessobjects.rebean.wi.CSVView;
import com.businessobjects.rebean.wi.DocumentInstance;
import com.businessobjects.rebean.wi.OutputFormatType;
import com.businessobjects.rebean.wi.Prompt;
import com.businessobjects.rebean.wi.Prompts;
import com.businessobjects.rebean.wi.ReportEngine;
import com.businessobjects.rebean.wi.ReportEngines;
import com.crystaldecisions.sdk.exception.SDKException;
import com.crystaldecisions.sdk.framework.CrystalEnterprise;
import com.crystaldecisions.sdk.framework.IEnterpriseSession;
import com.crystaldecisions.sdk.framework.ISessionMgr;
import com.crystaldecisions.sdk.occa.infostore.IInfoObject;
import com.crystaldecisions.sdk.occa.infostore.IInfoObjects;
import com.crystaldecisions.sdk.occa.infostore.IInfoStore;
public class bo_util {
private final static Logger goLog = Logger.getLogger(bo_util.class.getName());
private static FileHandler fh = null;
public static IEnterpriseSession iSess = null;
public static ReportEngines reportEngines = null;
public static ReportEngine wiRepEngine = null;
public static String username = "";
public static String pwd = "";
public static String cmsname = "serveur";
public static String authtype = "secEnterprise";
public static String err = "";
public static IInfoObject rppEncours = null;
public static void init(){
try {
fh = new FileHandler("log_ex1.log", false);
}catch (SecurityException | IOException e){
e.printStackTrace();
}
Logger l = Logger.getLogger("");
fh.setFormatter(new SimpleFormatter());
l.addHandler(fh);
l.setLevel(Level.CONFIG);
}
public static void main(String[] args) {
bo_util.init();
}
public static Boolean connectBO() {
System.out.println("Connecting...");
ISessionMgr sessionMgr;
try {
sessionMgr = CrystalEnterprise.getSessionMgr();
iSess = sessionMgr.logon(username, pwd, cmsname, authtype);
System.out.println("Connected!");
return true;
} catch (SDKException e) {
goLog.log(Level.INFO, "NOT Connected ------");
return false;
}
/*
* finally { if (reportEngines != null) reportEngines.close(); if (iSess
* != null) iSess.logoff(); }
*/
}
public static void deconnect() {
if (reportEngines != null)
reportEngines.close();
if (iSess != null)
iSess.logoff();
System.out.println("Finished!");
}
public static Vector<Integer> getIDrpp(String nomRpp, String chemin) throws SDKException{
Vector<Integer> ret = new Vector<Integer>();
IInfoStore infostore = (IInfoStore) iSess.getService("InfoStore");
String query = "select si_id, si_name, si_parentid from CI_INFOOBJECTS where SI_NAME LIKE '%" + nomRpp + "%' and SI_KIND in ('Webi','FullClient') and SI_INSTANCE=0";
IInfoObjects infoObjects = (IInfoObjects) infostore.query(query);
for (Object object : infoObjects) {
IInfoObject infoObject = (IInfoObject) object;
String path = getInfoObjectPath(infoObject);
if (path.startsWith("/") & !path.startsWith("/Web") & !path.startsWith("/Mon") & !path.startsWith("/Report") & !path.startsWith("/User")) {
Integer id_doc = infoObject.getID();
if (chemin == "" || chemin.equals(path))
ret.add(id_doc);
else
goLog.log(Level.WARNING, "Le chemin ne correspond pas");
rppEncours = infoObject;
}
}
return ret;
}
public static boolean lanceRefresh(int idrpp, String nomRpp, String ValPrompt, String Dest, String export) throws SDKException{
reportEngines = (ReportEngines) iSess.getService("ReportEngines");
ReportEngine wiRepEngine = (ReportEngine) reportEngines.getService(ReportEngines.ReportEngineType.WI_REPORT_ENGINE);
DocumentInstance widoc = wiRepEngine.openDocument(idrpp);
Prompts p;
p = widoc.getPrompts();
Integer nbP = p.getCount();
String [] aValPrompt = ValPrompt.split("/");
if (nbP > 0) {
System.out.println("Nombre d'argument : " + p.getCount());
if (nbP > 0) {
Prompt pro0 = p.getItem(0);
System.out.println("Prompt name 1 : " + pro0.getID());
String[] dte0 = new String[1];
//dte0[0] = "20035209";
dte0[0] = aValPrompt[0];
pro0.enterValues(dte0);
System.out.println("Valeur prompt 1 : " + pro0.getCurrentValues()[0]);
if (nbP > 1) {
Prompt pro1 = p.getItem(1);
System.out.println("Prompt name 2 : " + pro1.getID());
String[] dte1 = new String[1];
//dte1[0] = "Livre";
dte1[0] = aValPrompt[1];
pro1.enterValues(dte1);
System.out.println("Valeur prompt 2 : " + pro1.getCurrentValues()[0]);
}
}
}
widoc.setPrompts();
widoc.refresh();
widoc.save();
if (widoc.getMustFillPrompts()){ // check si tous les prompts ont été rempli
System.out.println("--> Check tous les prompts : ok");
}
if (widoc.getMustFillContexts()){ // check si context not seleted
System.out.println("--> Error : Context has not been selected");
err = "Error : Context has not been selected";
widoc.closeDocument();
return false;
}
if (export != "" || export != "none"){
BinaryView bv = null;
switch (export){
case "pdf":
bv = (BinaryView) widoc.getView(OutputFormatType.PDF);
writeBytes(bv.getContent(), nomRpp + "." + export);
break;
case "csv":
CSVView csvView = null;
csvView = (CSVView) widoc.getDataProviders().getView(OutputFormatType.CSV);
File f = new File(nomRpp + "." + export);
FileWriter fw;
try {
fw = new FileWriter(f);
fw.write(csvView.getContent());
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
csvView = null;
break;
case "xlsx":
bv = (BinaryView) widoc.getView(OutputFormatType.XLSX);
writeBytes(bv.getContent(), nomRpp + "." + export);
break;
default :
err = "Format d'export non supporté";
widoc.closeDocument();
return false;
}
bv = null;
}
widoc.closeDocument();
widoc = null;
System.out.println("----- FIN TRAITEMENT REFRESH RAPPORT (" + nomRpp + ")-----");
/*if (Dest != ""){
IInfoStore infostore = (IInfoStore) iSess.getService("InfoStore");
String folderquery = "select si_id from CI_INFOOBJECTS WHERE SI_KIND='Folder' and SI_NAME='" + Dest + "'";
IInfoObjects folders = infostore.query(folderquery);
IInfoObject folder = (IInfoObject) folders.get(0);
IInfoObjects newInfoObjects = infostore.newInfoObjectCollection();
IPublication publication = (IPublication) newInfoObjects.add(IPublication.KIND);
publication.setTitle("Test Publication");
publication.setDescription("publication test");
publication.setParentID(folder.getID());
publication.save();
}*/
return true;
}
public static void writeBytes(byte[] data, String nomfich){
goLog.log(Level.INFO, "ecrit nomfich : " + nomfich);
try {
File file = new File(nomfich);
FileOutputStream fstream;
fstream = new FileOutputStream(file);
fstream.write(data);
fstream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static String getInfoObjectPath(IInfoObject infoObject) throws SDKException {
String path = "";
while (infoObject.getParentID() != 0) {
infoObject = infoObject.getParent();
path = "/" + infoObject.getTitle() + path;
}
return path;
}
} |
Partager