
h1. Taxonomy Migration from 2.2.x to 2.5



h1. Create a Groovy Script
We need to create an action script in order to process the taxonomy migration<br />
Go to ECMAmin portlet > Advanced configuration > Manage Scripts > Click on the add button. <br />
Copy the content below into the field "Script contents" and enter "MigrateTaxonomiesAction.groovy" in the field "Script name". Save the script action.
{code}
/*
 * Copyright (C) 2003-2008 eXo Platform SAS.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see<http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.NodeIterator;

import org.apache.commons.logging.Log;
import org.exoplatform.services.cms.scripts.CmsScript;
import org.exoplatform.services.jcr.RepositoryService;
import org.exoplatform.services.jcr.core.ManageableRepository;
import org.exoplatform.services.jcr.ext.app.SessionProviderService;
import org.exoplatform.services.cms.taxonomy.TaxonomyService;
import org.exoplatform.services.log.ExoLogger;

/**
* This script will be used to migrate taxonomies
*/
public class MigrateTaxonomiesAction implements CmsScript {

  private RepositoryService repositoryService_;
  private SessionProviderService seProviderService_;
	private TaxonomyService taxonomyService;
  private static final Log LOG  = ExoLogger.getLogger("ScriptAction.MigrateTaxonomies");

  public MigrateTaxonomiesAction(RepositoryService repositoryService,
      SessionProviderService sessionProviderService, TaxonomyService taxonomyService) {
    this.repositoryService_ = repositoryService;
    seProviderService_ = sessionProviderService;
		this.taxonomyService = taxonomyService;
  }

  public List<String> getPropertyValue(Property prop) throws Exception {
    List<String> uuids = new ArrayList<String>();
    if(prop.getDefinition() != null && prop.getDefinition().isMultiple()) {
      Value[] values =  prop.getValues();
      for (Value value : values) {
        if(value.getString().length() > 0) uuids.add(value.getString());
      }
    }
    return uuids;
  }

  public void execute(Object context) throws Exception {
    Map variables = (Map) context;
    String nodePath = (String)variables.get("nodePath") ;
    String repository = (String)variables.get("repository");
    String srcWorkspace = (String)variables.get("srcWorkspace");
    ManageableRepository manageableRepository = repositoryService_.getRepository(repository);
    Session session = seProviderService_.getSystemSessionProvider(null).getSession(srcWorkspace, manageableRepository);
    Node node = (Node)session.getItem(nodePath);
    processMigrateDatas(node, manageableRepository);
    session.save();
    session.logout();
  }
	
  private void processMigrateDatas(Node node, ManageableRepository manageableRepository) throws Exception {
    if(node.hasProperty("exo:category")) {
     Property category = node.getProperty("exo:category");
     List<String> uuids = getPropertyValue(category);
     Session sessionByWs = null;
     Node categoryNode = null;
     Node linkNode = null;
     for(String workspace : manageableRepository.getWorkspaceNames()) {
       sessionByWs = seProviderService_.getSystemSessionProvider(null).getSession(workspace, manageableRepository);
       for(String uuid : uuids) {
        try {
	  categoryNode = sessionByWs.getNodeByUUID(uuid);
	  try {
	    LOG.info("Migrating the node '"+node.getPath()+"' with category '"+categoryNode.getPath()+"' ")
	    taxonomyService.addCategory(node, "System", categoryNode.getPath());
	  } catch(Exception e) {
	    LOG.error("MIGRATE FAILED! Cannot migrate for node '" +node.getPath()+ "'", e);	
	  }
	} catch(Exception ne) {
	  continue;
	}
     }

     sessionByWs.save();
     sessionByWs.logout();
    }
    node.removeMixin("exo:categorized");
  } else if(node.hasNodes()) {
    NodeIterator nodeIter = node.getNodes();
    while(nodeIter.hasNext()) {
      processMigrateDatas(nodeIter.nextNode(), manageableRepository);
    }
  } else {
    return;
  }
 }
	
 public void setParams(String[] params) {}

}
{code}

[Download this file|http://wiki.exoplatform.org/xwiki/bin/download/ECM/Taxonomy+Migration+from+ECM+2%2D2%2Dx+to+DMS+2%2D5/MigrateTaxonomiesAction.groovy]




h1. Create an Action Type
Go to ECMAmin portlet > Advanced configuration > Create an Action Type > Click on the add button. <br />
The Action Type Form is shown.
Enter the name _exo:migrateTaxonomiesAction_ and choose the _MigrateTaxonomiesAction.groovy_ action script in the execute drop down list.<br />

<div style="text-align:center;">
!actiontypeform.jpg!
</div>


h1. Add a Template for the Action Type
Go to ECMAdmin portlet > Content Presentation > Manage templates > Click on the add button. <br />
The template form is shown.

<div style="text-align:center;">

!templateform.jpg!
</div>
Choose the dialog tab an copy the code shown underneath into the diaglog field.

{code}
<%
	def repository = uicomponent.getRepositoryName();
	String[] repositoryField = ["jcrPath=/node/exo:repository", "visible=false", repository] ;
	uicomponent.addHiddenField("repository", repositoryField) ;
%>
<div class="UIFormWithTitle">
  <div class="TitleBar"><%=_ctx.appRes(uicomponent.getId() + ".title")%></div>
	<% uiform.begin() %>
  <div class="HorizontalLayout">	
		<table class="UIFormGrid">	
  		<tr>
	      <td class="FieldLabel"><%=_ctx.appRes("ScriptAction.dialog.label.id")%></td>
	      <td class="FieldComponent">
		      <%
		        String[] fieldId = ["jcrPath=/node", "mixintype=exo:move", "editable=false", "visible=if-not-null"] ;
		        uicomponent.addMixinField("id", fieldId) ;
		      %>
		    </td>
		  </tr>
			<tr>
			  <td class="FieldLabel"><%=_ctx.appRes("ScriptAction.dialog.label.name")%></td>
			  <td class="FieldComponent">
				  <%
						String[] fieldName = ["jcrPath=/node/exo:name", "validate=empty"];
						uicomponent.addTextField("actionName", fieldName);
				  %>
				</td>
			</tr>		          					
			<tr>
			  <td class="FieldLabel"><%=_ctx.appRes("ScriptAction.dialog.label.lifecycle")%></td>
			  <td class="FieldComponent">
				  <%
						String[] fieldLifecycle = ["jcrPath=/node/exo:lifecyclePhase", "options=add"] ;
						uicomponent.addSelectBoxField("lifecycle", fieldLifecycle)
				  %>
				</td>
			</tr>
		  <tr>
				<td class="FieldLabel">Roles</td>
				<td class="FieldComponent">
					<%
						String[] fieldRoles = ["jcrPath=/node/exo:roles", "selectorAction=selectPermission", "selectorClass=org.exoplatform.ecm.webui.selector.UIPermissionSelector", "selectorIcon=Permission24x24Icon", "validate=empty", "editable=false"] ;
						uicomponent.addActionField("roles", fieldRoles) ;
					%>
				</td>
			</tr>			

 		</table>
		<%/* start render action*/%>
		<div class="UIAction">
			<table align="center" class="ActionContainer">
				<tr>
					<td align="center">
					  <% for(action in uicomponent.getActions()) {
					  		 String actionLabel = _ctx.appRes(uicomponent.getName() + ".action." + action)
								 String link = uicomponent.event(action) ;
					  %>
						  <a href="$link" class="ActionButton LightBlueStyle">
								<div class="ButtonLeft">
								  <div class="ButtonRight">
									  <div class="ButtonMiddle">
											$actionLabel
								    </div>
								  </div>
							  </div>
						  </a>
					  <%}%>
			  	</td>
			  </tr>
			</table>
		</div>
		<%/* end render action*/%>
  </div>
  <%uiform.end()%>
</div>
{code}
[Download this file|http://wiki.exoplatform.org/xwiki/bin/download/ECM/Taxonomy+Migration+from+ECM+2%2D2%2Dx+to+DMS+2%2D5/dialog1.gtmpl]

You can leave the view field with content is "test" or anything you want. Click save button to commit the changes.


h1. Replace new BaseActionLauncherListener class
  [Download this file|http://wiki.exoplatform.org/xwiki/bin/download/ECM/Taxonomy+Migration+from+ECM+2%2D2%2Dx+to+DMS+2%2D5/BaseActionLauncherListener.java]
 to replace the old BaseActionLaucherListener class


h1. Import Data with References to the Imported Taxonomies
- Import all taxonomies which you exported from an old version of DMS into dms-system:/exo:ecm/exo:taxonomyTrees/storage/System. Make sure you don't have any same name taxonomy node in this tree. You can do it easy by remove the default taxonomies(calendar, cms) before do the import action. In the import form, you have to choose the UUID behavior is "Remove existing" to ensure the category is the same as before.
!importtaxonomies.jpg!
- In the collaboration workspace create a folder.(For ex: "Migrate datas").
- Add an action for this folder using the action type "exo:migrateTaxonomiesAction"  and the lifecycle "Add".
- Import all data with references to imported taxonomies into the folder "Migrate datas". In this step, you can choose the UUID behavior is "Create new"
