2424import edu .caltech .ipac .table .GroupInfo ;
2525import edu .caltech .ipac .table .JsonTableUtil ;
2626import edu .caltech .ipac .table .ParamInfo ;
27+ import edu .caltech .ipac .table .ResourceInfo ;
2728import edu .caltech .ipac .table .TableMeta ;
2829import edu .caltech .ipac .table .TableUtil ;
2930
3334import java .util .List ;
3435import java .util .stream .Collectors ;
3536
37+ import static edu .caltech .ipac .firefly .data .sofia .VOSpectraModel .SPECTRADM_UTYPE ;
3638import static edu .caltech .ipac .util .StringUtils .applyIfNotEmpty ;
3739import static edu .caltech .ipac .util .StringUtils .isEmpty ;
3840
@@ -48,8 +50,11 @@ public class MultiSpectrumProcessor extends EmbeddedDbProcessor {
4850 public static final String MODE = "mode" ;
4951 public static final String SEL_ROW_IDX = "sel_row_idx" ;
5052 public static final String SPECTR_IDX = "spectr_idx" ;
53+ public static final String MULTI_SPECTRUM_UTYPE = "ipac:MultiSpectrum" ; // utype for the MultiSpectrum table
54+ public static final String MULTI_SPECTRUM_SERVICE_UTYPE = MULTI_SPECTRUM_UTYPE + "-service" ;
55+ public static final String SPECTRUM_DATA_UTYPE = SPECTRADM_UTYPE + ".Data" ; // utype for the spectrum data table
5156
52- private enum Mode { fetch , // retrieve the MultiSpectrum table, return data product table
57+ public enum Mode { fetch , // retrieve the MultiSpectrum table, return data product table
5358 links , // return links table to the spectrums
5459 extract // extract the spectrum from array data, then return a SpectralDM table.
5560 };
@@ -90,21 +95,24 @@ protected DataGroupPart getResultSet(TableServerRequest treq, DbAdapter dbAdapte
9095//====================================================================
9196
9297
93- record MultiSpecInfo (DataGroup table , List <DataType > metaCols , List <GroupInfo > spectrums ){}
98+ record MultiSpecInfo (DataGroup table , List <DataType > metaCols , List <GroupInfo > spectrums , List < ResourceInfo > services ){}
9499
95100
96101 private static MultiSpecInfo findSpectrums (TableServerRequest treq , DbAdapter dbAdapter ) throws DataAccessException {
97102 DataGroup table = dbAdapter .getHeaders (dbAdapter .getDataTable ());
98103
99- if (!table .getAttribute (TableMeta .UTYPE , "" ).equalsIgnoreCase ("ipac:MultiSpectrum" )) return null ;
104+ if (!table .getAttribute (TableMeta .UTYPE , "" ).equalsIgnoreCase (MULTI_SPECTRUM_UTYPE )) return null ;
100105
101106 List <GroupInfo > spectrums = table .getGroupInfos ().stream ()
102107 .filter (g -> String .valueOf (g .getUtype ()).toLowerCase ().matches ("(?i)ipac:(Spectrum\\ .)?ArrayData" ))
103108 .collect (Collectors .toList ());
104109 List <DataType > metaCols = Arrays .stream (table .getDataDefinitions ())
105110 .filter (dt -> !dt .isArrayType ()).collect (Collectors .toList ());
111+ List <ResourceInfo > services = table .getResourceInfos ().stream ()
112+ .filter (ri -> MULTI_SPECTRUM_SERVICE_UTYPE .equalsIgnoreCase (String .valueOf (ri .getUtype ())))
113+ .collect (Collectors .toList ());
106114
107- return new MultiSpecInfo (table , metaCols , spectrums );
115+ return new MultiSpecInfo (table , metaCols , spectrums , services );
108116 }
109117
110118 private DataGroupPart createSpectrumTable (TableServerRequest treq , DbAdapter dbAdapter , MultiSpecInfo specs ) throws DataAccessException {
@@ -135,9 +143,19 @@ private DataGroupPart createSpectrumTable(TableServerRequest treq, DbAdapter dbA
135143 }
136144
137145 // add SpectralDM meta
138- table .addAttribute (TableMeta .UTYPE , "spec:Spectrum" );
139- selSpec .setUtype ("spec:Spectrum.Data" );
140- table .setGroupInfos (Arrays .asList (selSpec ));
146+ table .getTableMeta ().setAttribute (TableMeta .UTYPE , SPECTRADM_UTYPE );
147+ selSpec .setUtype (SPECTRUM_DATA_UTYPE );
148+ table .setGroupInfos (List .of (selSpec ));
149+
150+ // copy service descriptors if any
151+ if (!specs .services .isEmpty ()) {
152+ table .setResourceInfos (
153+ specs .services .stream ()
154+ .filter (ri -> ri .getUtype ().equalsIgnoreCase (MULTI_SPECTRUM_SERVICE_UTYPE ))
155+ .map (ResourceInfo ::copyOf )
156+ .peek (ri -> ri .setUtype ("adhoc:service" ))
157+ .toList ());
158+ }
141159
142160 // therefore, convert all paramRefs to columnRefs
143161 table .getGroupInfos ().forEach (gInfo -> {
@@ -147,8 +165,6 @@ private DataGroupPart createSpectrumTable(TableServerRequest treq, DbAdapter dbA
147165 gInfo .setParamRefs (null );
148166 });
149167
150-
151-
152168 return new DataGroupPart (table , 0 , table .size ());
153169 }
154170
@@ -200,7 +216,7 @@ private DataGroupPart createDataProductTable(TableServerRequest treq, DbAdapter
200216 .filter (gi -> !"ipac:Spectrum.ArrayData" .equalsIgnoreCase (String .valueOf (gi .getUtype ())))
201217 .collect (Collectors .toList ())); // remove all Spectrum.ArrayData groups;
202218 table .setResourceInfos (table .getResourceInfos ().stream ()
203- .filter (ri -> !"ipac:MultiSpectrum" .equalsIgnoreCase (String .valueOf (ri .getUtype ())))
219+ .filter (ri -> !MULTI_SPECTRUM_UTYPE .equalsIgnoreCase (String .valueOf (ri .getUtype ())))
204220 .collect (Collectors .toList ())); // remove all ipac:MultiSpectrum resources;
205221
206222 for (int i = 0 ; i < table .size (); i ++) {
@@ -281,16 +297,16 @@ private DataGroup transformArrayToRows(DataGroup table, List<GroupInfo.RefInfo>
281297 Logger .getLogger ().warn ("MultiSpectrumProcessor:transformArrayToRows is expecting only 1 row but received: " + table .size ());
282298 }
283299 Object [] cary = table .get (0 ).getData ();
284- if (!cary [0 ].getClass ().isArray ()) {
285- Logger .getLogger ().error ("MultiSpectrumProcessor:transformArrayToRows: table cell is not an array" );
286- return null ;
287- }
288300 int nrows = Array .getLength (cary [cary .length -1 ]); // assuming the added data has the right values. need to find a better way to handle this.
289301 for (int i = 0 ; i < nrows ; i ++) {
290302 Object [] rowData = new Object [cols .size ()];
291303 int csize = (cols .size () - params .size ());
292304 for (int c = 0 ; c < csize ; c ++) {
293- rowData [c ] = getAryVal (cary , c , i );;
305+ if (cary [c ].getClass ().isArray ()) {
306+ rowData [c ] = getAryVal (cary , c , i );;
307+ } else {
308+ rowData [c ] = cary [c ]; // not an array, use same value for all rows.
309+ }
294310 }
295311 for (int c = 0 ; c < params .size (); c ++) {
296312 ParamInfo pinfo = table .getParam (toCname (params .get (c ).getRef (), table ));
@@ -300,6 +316,7 @@ private DataGroup transformArrayToRows(DataGroup table, List<GroupInfo.RefInfo>
300316 }
301317 return ntable ;
302318 }
319+
303320 private Object getAryVal (Object [] aryOfAry , int oIdx , int idx ) {
304321 try {
305322 return getAryVal (aryOfAry [oIdx ], idx );
0 commit comments