public DriftChangeSetSummary storeChangeSetInNewTransaction(Subject subject, final int resourceId,
final File changeSetZip, final List<JPADriftFile> driftFilesToRequest, final Headers[] headers)
throws Exception {
final Resource resource = getResource(resourceId);
final DriftChangeSetSummary summary = new DriftChangeSetSummary();
final boolean storeBinaryContent = isBinaryContentStorageEnabled();
try {
ZipUtil.walkZipFile(changeSetZip, new ChangeSetFileVisitor() {
@Override
public boolean visit(ZipEntry zipEntry, ZipInputStream stream) throws Exception {
JPADriftChangeSet driftChangeSet;
ChangeSetReader reader = new ChangeSetReaderImpl(new BufferedReader(new InputStreamReader(stream)),
false);
// store the new change set info (not the actual blob)
DriftDefinition driftDef = findDriftDefinition(resource, reader.getHeaders());
if (driftDef == null) {
LOG.error("Unable to locate DriftDefinition for Resource [" + resource
+ "]. Change set cannot be saved.");
return false;
}
// TODO: Commenting out the following line for now. We want to set the
// version to the value specified in the headers, but we may want to also
// validate it against the latest version we have in the database so that
// we can make sure that the agent is in sync with the server.
//
//int version = getChangeSetVersion(resource, config);
int version = reader.getHeaders().getVersion();
DriftChangeSetCategory category = reader.getHeaders().getType();
driftChangeSet = new JPADriftChangeSet(resource, version, category, driftDef);
entityManager.persist(driftChangeSet);
summary.setCategory(category);
summary.setResourceId(resourceId);
summary.setDriftDefinitionName(reader.getHeaders().getDriftDefinitionName());
summary.setDriftHandlingMode(driftDef.getDriftHandlingMode());
summary.setCreatedTime(driftChangeSet.getCtime());
if (version > 0) {
for (FileEntry entry : reader) {
boolean addToList = storeBinaryContent || !DriftUtil.isBinaryFile(entry.getFile());
JPADriftFile oldDriftFile = getDriftFile(entry.getOldSHA(), driftFilesToRequest, addToList);
JPADriftFile newDriftFile = getDriftFile(entry.getNewSHA(), driftFilesToRequest, addToList);
// TODO Figure out an efficient way to save coverage change sets.
// The initial/coverage change set could contain hundreds or even thousands
// of entries. We probably want to consider doing some kind of batch insert
//
// jsanda
// use a path with only forward slashing to ensure consistent paths across reports
String path = FileUtil.useForwardSlash(entry.getFile());
JPADrift drift = new JPADrift(driftChangeSet, path, entry.getType(), oldDriftFile,
newDriftFile);
entityManager.persist(drift);
// we are taking advantage of the fact that we know the summary is only used by the server
// if the change set is a DRIFT report. If its a coverage report, it is not used (we do
// not alert on coverage reports) - so don't waste memory by collecting all the paths
// when we know they aren't going to be used anyway.
if (category == DriftChangeSetCategory.DRIFT) {
summary.addDriftPathname(path);
}
}
} else {
summary.setInitialChangeSet(true);
JPADriftSet driftSet = new JPADriftSet();
for (FileEntry entry : reader) {
boolean addToList = storeBinaryContent || !DriftUtil.isBinaryFile(entry.getFile());
JPADriftFile newDriftFile = getDriftFile(entry.getNewSHA(), driftFilesToRequest, addToList);
String path = FileUtil.useForwardSlash(entry.getFile());