|
22 | 22 | import org.labkey.api.action.ReadOnlyApiAction; |
23 | 23 | import org.labkey.api.action.SpringActionController; |
24 | 24 | import org.labkey.api.data.Container; |
| 25 | +import org.labkey.api.data.DbScope; |
25 | 26 | import org.labkey.api.data.TableInfo; |
26 | 27 | import org.labkey.api.exp.api.ExpData; |
27 | 28 | import org.labkey.api.exp.api.ExperimentService; |
@@ -133,64 +134,68 @@ public ApiResponse execute(SignalDataResourceForm form, BindException errors) th |
133 | 134 | List<Map<String, String>> results = new ArrayList<>(); |
134 | 135 | Container c = getContainer(); |
135 | 136 | FileContentService svc = FileContentService.get(); |
136 | | - TableInfo ti = ExpSchema.TableType.Data.createTable(new ExpSchema(getUser(), c), ExpSchema.TableType.Data.toString(), null); |
| 137 | + TableInfo ti = new ExpSchema(getUser(), getContainer()).getDatasTable(); |
137 | 138 | QueryUpdateService qus = ti.getUpdateService(); |
138 | | - int maxUrlSize = ExperimentService.get().getTinfoData().getColumn("DataFileURL").getScale(); |
| 139 | + int maxUrlSize = ExperimentService.get().getTinfoData().getColumn(ExpDataTable.Column.DataFileUrl.name()).getScale(); |
139 | 140 | int idx = 0; |
140 | 141 |
|
141 | | - for (String path : form.getPaths()) |
| 142 | + try (DbScope.Transaction transaction = DbScope.getLabKeyScope().ensureTransaction()) |
142 | 143 | { |
143 | | - WebdavResource resource = WebdavService.get().lookup(path); |
144 | | - String fileName = form.getFiles().get(idx++); |
145 | | - |
146 | | - if (null != resource) |
| 144 | + for (String path : form.getPaths()) |
147 | 145 | { |
148 | | - ExpData data = svc.getDataObject(resource, c); |
149 | | - if (data == null) |
| 146 | + WebdavResource resource = WebdavService.get().lookup(path); |
| 147 | + String fileName = form.getFiles().get(idx++); |
| 148 | + |
| 149 | + if (null != resource) |
150 | 150 | { |
151 | | - // create the ExpData object if it doesn't already exist |
152 | | - File file = resource.getFile(); |
153 | | - if (null != file) |
| 151 | + ExpData data = svc.getDataObject(resource, c); |
| 152 | + if (data == null) |
154 | 153 | { |
155 | | - data = ExperimentService.get().createData(c, UPLOADED_FILE); |
156 | | - data.setName(file.getName()); |
157 | | - data.setDataFileURI(file.toURI()); |
158 | | - |
159 | | - String dataFileURL = data.getDataFileUrl(); |
160 | | - if (dataFileURL == null || dataFileURL.length() <= maxUrlSize) |
161 | | - { |
162 | | - data.save(getUser()); |
163 | | - } |
164 | | - else |
| 154 | + // create the ExpData object if it doesn't already exist |
| 155 | + File file = resource.getFile(); |
| 156 | + if (null != file) |
165 | 157 | { |
166 | | - throw new ValidationException(String.format("The data file URL is too long to store in the database (max %d).", maxUrlSize)); |
| 158 | + data = ExperimentService.get().createData(c, UPLOADED_FILE); |
| 159 | + data.setName(file.getName()); |
| 160 | + data.setDataFileURI(file.toURI()); |
| 161 | + |
| 162 | + String dataFileURL = data.getDataFileUrl(); |
| 163 | + if (dataFileURL == null || dataFileURL.length() <= maxUrlSize) |
| 164 | + { |
| 165 | + data.save(getUser()); |
| 166 | + } |
| 167 | + else |
| 168 | + { |
| 169 | + throw new ValidationException(String.format("The data file URL is too long to store in the database (max %d).", maxUrlSize)); |
| 170 | + } |
167 | 171 | } |
168 | 172 | } |
169 | | - } |
170 | | - |
171 | | - if (null != data) |
172 | | - { |
173 | | - File canonicalFile = FileUtil.getAbsoluteCaseSensitiveFile(resource.getFile()); |
174 | | - String url = canonicalFile.toURI().toURL().toString(); |
175 | | - List<Map<String, Object>> rows = qus.getRows(getUser(), c, Collections.singletonList(Map.of(ExpDataTable.Column.DataFileUrl.name(), url))); |
176 | 173 |
|
177 | | - if (rows.size() == 1) |
| 174 | + if (null != data) |
178 | 175 | { |
179 | | - Map<String, String> props = new HashMap<>(); |
180 | | - props.put("FilePath", path); |
181 | | - props.put("FileName", fileName); |
182 | | - results.add(props); |
183 | | - for (Map.Entry<String, Object> entry : rows.get(0).entrySet()) |
| 176 | + File canonicalFile = FileUtil.getAbsoluteCaseSensitiveFile(resource.getFile()); |
| 177 | + String url = canonicalFile.toURI().toURL().toString(); |
| 178 | + List<Map<String, Object>> rows = qus.getRows(getUser(), c, Collections.singletonList(Map.of(ExpDataTable.Column.DataFileUrl.name(), url))); |
| 179 | + |
| 180 | + if (rows.size() == 1) |
184 | 181 | { |
185 | | - Object value = entry.getValue(); |
186 | | - if (null != value) |
187 | | - props.put(entry.getKey(), String.valueOf(value)); |
| 182 | + Map<String, String> props = new HashMap<>(); |
| 183 | + props.put("FilePath", path); |
| 184 | + props.put("FileName", fileName); |
| 185 | + results.add(props); |
| 186 | + for (Map.Entry<String, Object> entry : rows.get(0).entrySet()) |
| 187 | + { |
| 188 | + Object value = entry.getValue(); |
| 189 | + if (null != value) |
| 190 | + props.put(entry.getKey(), String.valueOf(value)); |
| 191 | + } |
188 | 192 | } |
| 193 | + else |
| 194 | + throw new RuntimeException(String.format("Unexpected number of rows returned for DataFileUrl '%s': %d", url, rows.size())); |
189 | 195 | } |
190 | | - else |
191 | | - throw new RuntimeException(String.format("Unexpected number of rows returned for DataFileUrl '%s': %d", url, rows.size())); |
192 | 196 | } |
193 | 197 | } |
| 198 | + transaction.commit(); |
194 | 199 | } |
195 | 200 | return new ApiSimpleResponse(Map.of("files", results)); |
196 | 201 | } |
|
0 commit comments