Skip to content

Commit

Permalink
implement /window/{windowId}/{documentId}/duplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Aug 29, 2017
1 parent 426ded2 commit 48b427a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand Down Expand Up @@ -254,7 +255,7 @@ else if (documentPath.isSingleIncludedDocument())
/**
*
* @param windowIdStr
* @param documentIdStr the string to identify the document to be returned. May also be {@link DocumentId#NEW_ID_STRING}, if a new record shall be created.
* @param documentIdStr the string to identify the document to be returned. May also be {@link DocumentId#NEW_ID_STRING}, if a new record shall be created.
* @param advanced
* @param events
* @return
Expand Down Expand Up @@ -310,8 +311,8 @@ private List<JSONDocument> patchDocument0(final DocumentPath documentPath, final
{
final IDocumentChangesCollector changesCollector = Execution.getCurrentDocumentChangesCollectorOrNull();
documentCollection.forDocumentWritable(
documentPath,
changesCollector,
documentPath,
changesCollector,
document -> {
document.processValueChanges(events, REASON_Value_DirectSetFromCommitAPI);
changesCollector.setPrimaryChange(document.getDocumentPath());
Expand All @@ -326,6 +327,20 @@ private List<JSONDocument> patchDocument0(final DocumentPath documentPath, final
return jsonDocumentEvents;
}

@PostMapping("/{windowId}/{documentId}/duplicate")
public JSONDocument duplicate(
@PathVariable("windowId") final String windowIdStr,
@PathVariable("documentId") final String documentIdStr,
@RequestParam(name = PARAM_Advanced, required = false, defaultValue = PARAM_Advanced_DefaultValue) final boolean advanced)
{
userSession.assertLoggedIn();

final DocumentPath fromDocumentPath = DocumentPath.rootDocumentPath(WindowId.fromJson(windowIdStr), DocumentId.of(documentIdStr));

final Document documentCopy = documentCollection.duplicateDocument(fromDocumentPath);
return JSONDocument.ofDocument(documentCopy, newJSONOptions().setShowAdvancedFields(advanced).build());
}

@DeleteMapping("/{windowId}/{documentId}")
public List<JSONDocument> deleteRootDocument(
@PathVariable("windowId") final String windowIdStr //
Expand Down Expand Up @@ -618,7 +633,6 @@ private JSONDocumentActionsList getDocumentActions(final DocumentPath documentPa
});
}


@GetMapping(value = "/{windowId}/{documentId}/{tabId}/{rowId}/references")
public JSONDocumentReferencesGroup getDocumentReferences(
@PathVariable("windowId") final String windowIdStr,
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/de/metas/ui/web/window/model/DocumentCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@
import org.adempiere.ad.expression.api.IExpressionEvaluator.OnVariableNotFound;
import org.adempiere.ad.expression.api.ILogicExpression;
import org.adempiere.ad.expression.api.LogicExpressionResult;
import org.adempiere.ad.persistence.TableModelLoader;
import org.adempiere.ad.trx.api.ITrx;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.model.CopyRecordFactory;
import org.adempiere.model.CopyRecordSupport;
import org.adempiere.model.InterfaceWrapperHelper;
import org.adempiere.model.PlainContextAware;
import org.adempiere.model.RecordZoomWindowFinder;
import org.adempiere.util.Check;
import org.adempiere.util.lang.IAutoCloseable;
import org.adempiere.util.lang.impl.TableRecordReference;
import org.compiere.model.PO;
import org.compiere.util.Env;
import org.compiere.util.Evaluatee;
import org.compiere.util.Evaluatees;
import org.slf4j.Logger;
Expand Down Expand Up @@ -577,6 +585,29 @@ public void invalidateDocumentByRecordId(final String tableName, final int recor
websocketSender.convertAndSend(event.getWebsocketEndpoint(), event);
});
}

public Document duplicateDocument(final DocumentPath fromDocumentPath)
{
final TableRecordReference fromRecordRef = getTableRecordReference(fromDocumentPath);

final Object fromModel = fromRecordRef.getModel(PlainContextAware.newWithThreadInheritedTrx());
final String tableName = InterfaceWrapperHelper.getModelTableName(fromModel);
final PO fromPO = InterfaceWrapperHelper.getPO(fromModel);


final PO toPO = TableModelLoader.instance.newPO(Env.getCtx(), tableName, ITrx.TRXNAME_ThreadInherited);
PO.copyValues(fromPO, toPO, true);
InterfaceWrapperHelper.save(toPO);

final CopyRecordSupport childCRS = CopyRecordFactory.getCopyRecordSupport(tableName);
childCRS.setAD_Window_ID(fromDocumentPath.getAD_Window_ID(-1));
childCRS.setParentPO(toPO);
childCRS.setBase(true);
childCRS.copyRecord(fromPO, ITrx.TRXNAME_ThreadInherited);

final DocumentPath toDocumentPath = DocumentPath.rootDocumentPath(fromDocumentPath.getWindowId(), DocumentId.of(toPO.get_ID()));
return forDocumentReadonly(toDocumentPath, NullDocumentChangesCollector.instance, Function.identity());
}

@Immutable
@Value
Expand Down

0 comments on commit 48b427a

Please sign in to comment.