Skip to content
This repository has been archived by the owner on Mar 31, 2019. It is now read-only.

Commit

Permalink
#186 - new ChnageName predef step
Browse files Browse the repository at this point in the history
  • Loading branch information
kovax committed Apr 28, 2018
1 parent 9c3b9df commit 91affdb
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* This file is part of the CRISTAL-iSE kernel.
* Copyright (c) 2001-2015 The CRISTAL Consortium. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*
* http://www.fsf.org/licensing/licenses/lgpl.html
*/
package org.cristalise.kernel.lifecycle.instance.predefined;

import java.util.Arrays;

import org.cristalise.kernel.common.CannotManageException;
import org.cristalise.kernel.common.InvalidDataException;
import org.cristalise.kernel.common.ObjectAlreadyExistsException;
import org.cristalise.kernel.common.ObjectCannotBeUpdated;
import org.cristalise.kernel.lookup.AgentPath;
import org.cristalise.kernel.lookup.DomainPath;
import org.cristalise.kernel.lookup.ItemPath;
import org.cristalise.kernel.lookup.Lookup.PagedResult;
import org.cristalise.kernel.lookup.Path;
import org.cristalise.kernel.process.Gateway;
import org.cristalise.kernel.utils.Logger;

public class ChangeName extends PredefinedStep {
public static final String description = "Removes Items old Name, add the new Name and changes the Name property";

public ChangeName() {
super();
}

@Override
protected String runActivityLogic(AgentPath agent, ItemPath item, int transitionID, String requestData, Object locker)
throws InvalidDataException, ObjectCannotBeUpdated, ObjectAlreadyExistsException, CannotManageException
{
String[] params = getDataList(requestData);

if (params.length != 2) throw new InvalidDataException("ChangeName: Invalid parameters: "+Arrays.toString(params));

String oldName = params[0];
String newName = params[1];

Logger.msg(3, "ChangeName - oldName:%s newName:%s", oldName, newName);

if (oldName.equals(newName)) {
Logger.msg(3, "ChangeName - oldName:%s == newName:%s - NOTHING DONE", oldName, newName);
return requestData;
}

PagedResult result = Gateway.getLookup().searchAliases(item, 0, 100);
DomainPath currentDP = null;

if (result.rows.size() > 0) {
for (Path path: result.rows) {
if (path.getName().equals(oldName)) {
currentDP = (DomainPath)path;
break;
}
}

if (currentDP == null) throw new InvalidDataException(item + " does not domainPath with name:" + oldName);
}
else
throw new InvalidDataException(item + " does not have any domainPath");

DomainPath rootDP = currentDP.getParent();

// Throws an exception if newName exists
Gateway.getLookupManager().add(new DomainPath(rootDP, newName));

try {
Gateway.getLookupManager().delete(currentDP);
}
catch (Exception e) {
Logger.error(e);

//recover original state
Gateway.getLookupManager().delete(new DomainPath(rootDP, newName));

throw new CannotManageException(e.getMessage());
}

try {
WriteProperty.write(item, "Name", newName, locker);
}
catch (Exception e) {
Logger.error(e);

//recover original state
Gateway.getLookupManager().delete(new DomainPath(rootDP, newName));
Gateway.getLookupManager().add( new DomainPath(rootDP, oldName));

throw new CannotManageException(e.getMessage());
}

return requestData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void createChildren() {
predInit("AddMemberToCollection", AddMemberToCollection.description, new AddMemberToCollection());
predInit("Import", "Imports an outcome into the Item, with a given schema and viewpoint", new Import());
predInit("CreateAgentFromDescription", "Create a new agent using this item as its description", new CreateAgentFromDescription());
predInit("ChangeName", ChangeName.description, new ChangeName());
}

public void predInit(String alias, String Description, PredefinedStep act) {
Expand Down

0 comments on commit 91affdb

Please # to comment.