1.- First see the instructions of this old post

2.- Change the file /src/main/resources/atlassian-plugin.xml like this. Bassically you need to add <projectTypeKey>business</projectTypeKey>

<atlassian-plugin key="${project.groupId}.${project.artifactId}" name="${project.name}" plugins-version="2">
    <plugin-info>
        <description>${project.description}</description>
        <version>${project.version}</version>
        <vendor name="${project.organization.name}" url="${project.organization.url}" />
        <param name="plugin-icon">images/pluginIcon.png</param>
        <param name="plugin-logo">images/pluginLogo.png</param>
    </plugin-info>

    <!-- add our i18n resource -->
    <resource type="i18n" name="i18n" location="my-project-template"/>

    <!-- add our web resources -->
    <web-resource key="my-project-template-resources" name="my-project-template Web Resources">
        <dependency>com.atlassian.auiplugin:ajs</dependency>

        <transformation extension="soy">
            <transformer key="soyTransformer"/>
        </transformation>

        <resource type="download" name="MyProjectTemplate.soy.js" location="/soy/MyProjectTemplate.soy"/>

        <resource type="download" name="images/" location="/images">
            <param name="content-type" value="image/png; charset=binary"/>
        </resource>

        <context>atl.general</context>
        <context>atl.admin</context>
    </web-resource>

    <project-blueprint key="my-project-template" weight="90">
        <label key="my.project.template.name"/>
		<projectTypeKey>business</projectTypeKey>
        <description key="my.project.template.description"/>
        <longDescription key="my.project.template.description.long"/>

        <infoPage soy-template="JIRA.Templates.ProjectTemplates.Tutorial.renderMyProjectTemplateExplanation" />

        <icon location="images/my-template-icon.png"/>
        <backgroundIcon location="images/my-template-background.png"/>

        <add-project>
            <hook class="com.example.projectTemplate.MyAddProjectHook"/>
            <descriptor file="/config/my-project-template-config.json"/>
        </add-project>
    </project-blueprint>

</atlassian-plugin>

3.- In the file /src/main/java/com/example/plugins/tutorial/MyAddProjectHook.java change the file to like this:

package com.example.projectTemplate;

//import com.atlassian.jira.blueprint.api.AddProjectHook;
import com.atlassian.jira.project.template.hook.AddProjectHook;
import com.atlassian.jira.project.template.*;
import com.atlassian.jira.project.template.hook.*;
//import com.atlassian.jira.blueprint.api.ConfigureData;
//import com.atlassian.jira.blueprint.api.ConfigureResponse;
//import com.atlassian.jira.blueprint.api.ValidateData;
//import com.atlassian.jira.blueprint.api.ValidateResponse;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import com.atlassian.jira.config.ConstantsManager;
import com.atlassian.jira.bc.projectroles.ProjectRoleService;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.fields.layout.field.*;
import com.atlassian.jira.issue.fields.screen.*;
import com.atlassian.jira.issue.fields.screen.issuetype.*;
import com.atlassian.jira.issue.issuetype.IssueType;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.IssueFieldConstants;
import com.atlassian.jira.issue.context.GlobalIssueContext;
import com.atlassian.jira.issue.customfields.CustomFieldSearcher;
import com.atlassian.jira.issue.customfields.CustomFieldType;
import com.atlassian.jira.issue.operation.IssueOperations;
import com.atlassian.jira.issue.operation.IssueOperations;
import com.atlassian.jira.issue.operation.ScreenableIssueOperation;
import com.atlassian.jira.notification.NotificationSchemeManager;
import com.atlassian.jira.permission.PermissionSchemeManager;
import com.atlassian.jira.scheme.SchemeManager;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.project.ProjectCategory;
import com.atlassian.jira.project.ProjectManager;
import com.atlassian.jira.scheme.Scheme;
import com.atlassian.jira.security.roles.ProjectRole;
import com.atlassian.jira.util.SimpleErrorCollection;
import com.atlassian.jira.workflow.WorkflowSchemeManager;
import com.atlassian.jira.util.SimpleErrorCollection;
import com.atlassian.sal.api.message.I18nResolver;
import org.ofbiz.core.entity.GenericEntityException;
import org.ofbiz.core.entity.GenericValue;

public class MyAddProjectHook implements AddProjectHook
{

	 private static final Logger LOGGER = LogManager.getLogger(MyAddProjectHook.class);

    @Override
    public ValidateResponse validate(final ValidateData validateData)
    {
        return ValidateResponse.create();
    }

    @Override
    public ConfigureResponse configure(final ConfigureData configureData)
    {

		Project project = configureData.project();
		ConstantsManager constantsManager = ComponentAccessor.getConstantsManager();
		String template_name = "Company Project Template";
		String template_new_name = project.getKey() + ":" + template_name;

		PermissionSchemeManager permissionSchemeManager = ComponentAccessor.getPermissionSchemeManager();
		Scheme permissionScheme = permissionSchemeManager.getSchemeObject(template_name);
        if (permissionScheme != null) {
			Scheme permissionSchemeNew = permissionSchemeManager.copyScheme(permissionScheme);
			permissionSchemeNew.setName(template_new_name);
			permissionSchemeManager.updateScheme(permissionSchemeNew);
            permissionSchemeManager.removeSchemesFromProject(project);
            permissionSchemeManager.addSchemeToProject(project, permissionSchemeNew);
        } 

		FieldLayoutManager fieldLayoutManager = ComponentAccessor.getFieldLayoutManager();
        EditableDefaultFieldLayout defaultFieldLayout = fieldLayoutManager.getEditableDefaultFieldLayout();

		EditableFieldLayout editableFieldLayout = new EditableFieldLayoutImpl(null, defaultFieldLayout.getFieldLayoutItems());
        editableFieldLayout.setName(template_new_name);
        editableFieldLayout.setDescription(template_new_name);
		FieldLayout myFieldLayout = fieldLayoutManager.storeAndReturnEditableFieldLayout(editableFieldLayout);

		FieldLayoutScheme fieldLayoutScheme = new FieldLayoutSchemeImpl(fieldLayoutManager, null);
        fieldLayoutScheme.setName(template_new_name);
        fieldLayoutScheme.setDescription(template_new_name);
		fieldLayoutScheme.store();

		FieldLayoutSchemeEntity fieldLayoutSchemeEntity = new FieldLayoutSchemeEntityImpl(fieldLayoutManager, null, constantsManager);
        fieldLayoutSchemeEntity.setIssueTypeId(null);
        fieldLayoutSchemeEntity.setFieldLayoutId(myFieldLayout.getId());
		//fieldLayoutSchemeEntity.store();
		fieldLayoutScheme.addEntity(fieldLayoutSchemeEntity);
		//fieldLayoutScheme.store();
		//This part not works fine 😦

		fieldLayoutScheme = null;
        for (FieldLayoutScheme scheme : fieldLayoutManager.getFieldLayoutSchemes()) {
            if (scheme.getName().equals(template_new_name)) {
                fieldLayoutScheme = scheme;
                break;
            }
        }
		fieldLayoutManager.addSchemeAssociation(project, fieldLayoutScheme.getId());

		FieldScreenManager fieldScreenManager = ComponentAccessor.getFieldScreenManager();
		FieldScreen fieldScreen =  new FieldScreenImpl(fieldScreenManager);
		fieldScreen.setName(template_new_name);
		fieldScreen.setDescription(template_new_name);
		fieldScreen.store();		

		FieldScreenTab defaultTab = fieldScreen.addTab("Details");

		FieldScreenSchemeManager fieldScreenSchemeManager = ComponentAccessor.getComponent(FieldScreenSchemeManager.class);
		FieldScreenSchemeImpl fieldScreenScheme = new FieldScreenSchemeImpl(fieldScreenSchemeManager, null);
        fieldScreenScheme.setName(template_new_name);
        fieldScreenScheme.setDescription(template_new_name);
        fieldScreenScheme.store();

        FieldScreenSchemeItemImpl fieldScreenSchemeItem = new FieldScreenSchemeItemImpl(fieldScreenSchemeManager, (GenericValue) null, fieldScreenManager);
        fieldScreenSchemeItem.setIssueOperation(null);
        fieldScreenSchemeItem.setFieldScreen(fieldScreenManager.getFieldScreen(fieldScreen.getId()));

        fieldScreenScheme.addFieldScreenSchemeItem(fieldScreenSchemeItem);

		IssueTypeScreenSchemeManager issueTypeScreenSchemeManager =  (IssueTypeScreenSchemeManager) ComponentManager.getComponentInstanceOfType(IssueTypeScreenSchemeManager.class);
		IssueTypeScreenScheme issueTypeScreenScheme = new IssueTypeScreenSchemeImpl(issueTypeScreenSchemeManager, null);
        issueTypeScreenScheme.setName(template_new_name);
        issueTypeScreenScheme.setDescription(template_new_name);
        issueTypeScreenScheme.store();

        IssueTypeScreenSchemeEntity issueTypeScreenSchemeEntity = new IssueTypeScreenSchemeEntityImpl(issueTypeScreenSchemeManager, (GenericValue) null, fieldScreenSchemeManager, constantsManager);
        issueTypeScreenSchemeEntity.setIssueTypeId(null);
        issueTypeScreenSchemeEntity.setFieldScreenScheme(fieldScreenSchemeManager.getFieldScreenScheme(fieldScreenScheme.getId()));
        issueTypeScreenScheme.addEntity(issueTypeScreenSchemeEntity);

		issueTypeScreenSchemeManager.addSchemeAssociation(project,issueTypeScreenScheme);

		// RETURN FINAL
		ConfigureResponse configureResponse = ConfigureResponse.create().setRedirect("/plugins/servlet/project-config/" + project.getKey() + "/summary");
		return configureResponse;

    }
}
Posted by:.

2 replies on “Tutorial – Creating a project template with Standalone artifacts in JIRA 7

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s