Commits

David Chui committed 16232c9

LBL-195 - Carpet bombing fix because I cannot replicate the damned issue on my computer even with Java(TM) SE Runtime Environment (build 1.6.0_22-b04-307-10M3261) from Apple. Moved the hack that was BackwardsCompatibilitySupport into a proper upgrade task that can be executed even if BackwardsCompatibilitySupport has been executed before. The new upgrade task is ctor injected, so no need for that pico shtff and it is somewhat fail fast (otherwise, if the required components could not be resolved, at least, the upgrade task would be disabled).

Comments (0)

Files changed (4)

src/main/java/com/atlassian/jira/plugin/labels/upgradetask/LegacyDataUpgradeTask.java

+package com.atlassian.jira.plugin.labels.upgradetask;
+
+import com.atlassian.jira.issue.CustomFieldManager;
+import com.atlassian.jira.issue.customfields.CustomFieldUtils;
+import com.atlassian.jira.issue.customfields.persistence.OfBizCustomFieldValuePersister;
+import com.atlassian.jira.issue.fields.CustomField;
+import com.atlassian.jira.ofbiz.OfBizDelegator;
+import com.atlassian.jira.plugin.labels.LabelsCFType;
+import com.atlassian.sal.api.message.Message;
+import com.atlassian.sal.api.upgrade.PluginUpgradeTask;
+import com.google.common.base.Predicate;
+import com.google.common.collect.Collections2;
+import org.ofbiz.core.entity.GenericValue;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+
+public class LegacyDataUpgradeTask implements PluginUpgradeTask
+{
+    private static final String PLUGIN_KEY = "com.atlassian.jira.plugin.labels";
+
+    private static final String ENTITY_CUSTOMFIELD_ID = "customfield";
+
+    private static final String TABLE_CUSTOMFIELD_VALUE = "CustomFieldValue";
+
+    private final OfBizDelegator ofBizDelegator;
+
+    private final CustomFieldManager customFieldManager;
+
+    public LegacyDataUpgradeTask(OfBizDelegator ofBizDelegator, CustomFieldManager customFieldManager)
+    {
+        this.ofBizDelegator = ofBizDelegator;
+        this.customFieldManager = customFieldManager;
+    }
+
+    @Override
+    public int getBuildNumber()
+    {
+        /*
+         * This is exactly the upgradeNumber that BackwardsCompatibilitySupport, although it may not be read from the same table.
+         * Anyways, it's looks to me that the upgrade task can be executed twice without harming anyone.
+         */
+        return 1;
+    }
+
+    @Override
+    public String getShortDescription()
+    {
+        return "https://studio.plugins.atlassian.com/browse/LBL-70";  //To change body of implemented methods use File | Settings | File Templates.
+    }
+
+    @Override
+    public Collection<Message> doUpgrade() throws Exception
+    {
+        Collection<CustomField> labelFields = Collections2.filter(
+                customFieldManager.getCustomFieldObjects(),
+                new Predicate<CustomField>()
+                {
+                    @Override
+                    public boolean apply(CustomField customField)
+                    {
+                        return customField.getCustomFieldType() instanceof LabelsCFType;
+                    }
+                }
+        );
+
+        for (final CustomField field : labelFields)
+        {
+            List<GenericValue> genericValues = ofBizDelegator.findByAnd(TABLE_CUSTOMFIELD_VALUE, new HashMap<String, Object>()
+            {
+                {
+                    put(OfBizCustomFieldValuePersister.ENTITY_VALUE_TYPE, null);
+                    put(ENTITY_CUSTOMFIELD_ID, CustomFieldUtils.getCustomFieldId(field.getId()));
+                }
+
+            });
+
+            for (GenericValue value : genericValues)
+            {
+                Object val = value.getString(OfBizCustomFieldValuePersister.FIELD_TYPE_STRING);
+                if (null != val)
+                    value.set(OfBizCustomFieldValuePersister.FIELD_TYPE_TEXT, val);
+            }
+
+            ofBizDelegator.storeAll(genericValues);
+        }
+
+        return Collections.emptyList();
+    }
+
+    @Override
+    public String getPluginKey()
+    {
+        return PLUGIN_KEY;
+    }
+}

src/main/java/com/atlassian/jira/plugin/labels/utils/BackwardsCompatibilitySupport.java

-package com.atlassian.jira.plugin.labels.utils;
-
-import com.atlassian.jira.ComponentManager;
-import com.atlassian.jira.extension.Startable;
-import com.atlassian.jira.issue.customfields.CustomFieldUtils;
-import com.atlassian.jira.issue.customfields.manager.DefaultGenericConfigManager;
-import com.atlassian.jira.issue.customfields.manager.GenericConfigManager;
-import com.atlassian.jira.issue.customfields.persistence.OfBizCustomFieldValuePersister;
-import com.atlassian.jira.issue.fields.CustomField;
-import com.atlassian.jira.issue.CustomFieldManager;
-import com.atlassian.jira.ofbiz.OfBizDelegator;
-import com.atlassian.jira.plugin.labels.LabelsCFType;
-import com.atlassian.jira.plugin.labels.utils.functor.Algorithms;
-import com.atlassian.jira.plugin.labels.utils.functor.UnaryPredicate;
-import org.apache.log4j.Logger;
-import org.ofbiz.core.entity.GenericValue;
-import org.picocontainer.PicoContainer;
-
-import java.util.*;
-
-/**
- * this class is intended to check for the necessity of upgrading label data when new versions change the serialized format
- */
-public class BackwardsCompatibilitySupport implements Startable
-{
-    private static final Logger logger = Logger.getLogger(BackwardsCompatibilitySupport.class);
-
-    private static final String UPGRADE_NUMBER = "labels-upgrade";
-    public static final String ENTITY_CUSTOMFIELD_ID = "customfield";  // copied from OfBizCustomFieldValuePersister because it's protected
-    public static final String TABLE_CUSTOMFIELD_VALUE = "CustomFieldValue";
-
-    public void start()
-    {
-        logger.debug("Starting Labels Backwards Compatibility Check.");
-
-        PicoContainer container = getContainer();
-        OfBizDelegator delegator = getOfBizDelegator(container);
-        GenericConfigManager configManager = createGenericConfigManager(delegator);
-
-        Integer upgrade = (Integer) configManager.retrieve("Integer", UPGRADE_NUMBER);
-        int upgradeNumber = upgrade != null ? upgrade.intValue() : 0;
-
-        logger.debug("Found label data at version " + upgradeNumber);
-
-        if (upgradeNumber < 1)  // perform LBL-70 based upgrades
-        {
-            logger.info("Upgrading Label Data to version 1.");
-
-            // get all of the label fields
-            Collection labelFields = Algorithms.filter(getCustomFieldManager().getCustomFieldObjects(), new UnaryPredicate()
-            {
-                public boolean test(Object param)
-                {
-                    CustomField field = (CustomField) param;
-                    return field.getCustomFieldType() instanceof LabelsCFType;
-                }
-            });
-
-            for (Iterator it = labelFields.iterator(); it.hasNext();)
-            {
-                CustomField field = (CustomField) it.next();
-
-                Map limitClause = new HashMap();
-                limitClause.put(OfBizCustomFieldValuePersister.ENTITY_VALUE_TYPE, null);
-                limitClause.put(ENTITY_CUSTOMFIELD_ID, CustomFieldUtils.getCustomFieldId(field.getId()));
-
-                List genericValues = delegator.findByAnd(TABLE_CUSTOMFIELD_VALUE, limitClause);
-
-                for (Iterator it2 = genericValues.iterator(); it2.hasNext();)
-                {
-                    GenericValue value = (GenericValue) it2.next();
-                    Object val = value.getString(OfBizCustomFieldValuePersister.FIELD_TYPE_STRING);
-                    if (val != null)
-                        value.set(OfBizCustomFieldValuePersister.FIELD_TYPE_TEXT, val);
-                }
-
-                delegator.storeAll(genericValues);
-            }
-        }
-
-        configManager.update("Integer", UPGRADE_NUMBER, new Integer(1));
-
-        logger.debug("Completed Labels Data Version Check/Upgrade.");
-    }
-
-    protected CustomFieldManager getCustomFieldManager() {
-        return ComponentManager.getInstance().getCustomFieldManager();
-    }
-
-    protected GenericConfigManager createGenericConfigManager(OfBizDelegator delegator) {
-        return new DefaultGenericConfigManager(delegator);
-    }
-
-    protected OfBizDelegator getOfBizDelegator(PicoContainer container) {
-        return (OfBizDelegator) container.getComponentInstanceOfType(OfBizDelegator.class);
-    }
-
-    protected PicoContainer getContainer() {
-        return ComponentManager.getInstance().getContainer();
-    }
-}

src/main/resources/atlassian-plugin.xml

                 package="com.atlassian.jira.plugin.labels" key="labels"/>
     </customfield-searcher>
 
-    <component key="backwardsCompatibilitySupport"
-               name="Label Backwards Compatibility"
-               class="com.atlassian.jira.plugin.labels.utils.BackwardsCompatibilitySupport">
-        <interface>com.atlassian.jira.extension.Startable</interface>
-    </component>
-
     <!--  Labels Dashboard Portlet -->
     <portlet key="labelsdashboardportlet"
              name="Labels Dashboard Portlet"
                class="com.atlassian.jira.plugin.labels.upgradetask.GadgetUpgradeTask" public="true">
         <interface>com.atlassian.sal.api.upgrade.PluginUpgradeTask</interface>
     </component>
+    <component key="legacyDataUpgradeTask" name="Legacy Label Data Upgrade Task"
+               class="com.atlassian.jira.plugin.labels.upgradetask.LegacyDataUpgradeTask" public="true">
+        <interface>com.atlassian.sal.api.upgrade.PluginUpgradeTask</interface>
+    </component>
 
     <keyboard-shortcut key="labelsKeyboardShortcut" i18n-name="labels.keyboard.shortcut.open.labels.dialog.name" name="Edit Labels Shortcut"
                        state='enabled'>

src/test/java/com/atlassian/jira/plugin/labels/utils/TestBackwardsCompatibilitySupport.java

-package com.atlassian.jira.plugin.labels.utils;
-
-import java.util.List;
-import java.util.Map;
-
-import org.jmock.Mock;
-import org.jmock.MockObjectTestCase;
-import org.picocontainer.PicoContainer;
-
-import com.atlassian.core.ofbiz.test.mock.MockGenericValue;
-import com.atlassian.core.util.map.EasyMap;
-import com.atlassian.jira.issue.CustomFieldManager;
-import com.atlassian.jira.issue.customfields.manager.GenericConfigManager;
-import com.atlassian.jira.issue.fields.CustomField;
-import com.atlassian.jira.ofbiz.OfBizDelegator;
-import com.atlassian.jira.plugin.labels.LabelsCFType;
-import com.atlassian.jira.util.EasyList;
-
-public class TestBackwardsCompatibilitySupport extends MockObjectTestCase {
-
-    private GenericConfigManager genericConfigManager;
-
-    private OfBizDelegator ofBizDelegator;
-
-    private CustomFieldManager customFieldManager;
-
-    public void testStart() {
-        Mock mockOfBizDelegator;
-        Mock mockGenericConfigManager;
-        Mock mockCustomField;
-        Mock mockCustomFieldManager;
-        CustomField customField;
-
-        mockOfBizDelegator = new Mock(OfBizDelegator.class);
-        mockOfBizDelegator.expects(exactly(2)).method("findByAnd").with(
-                eq("CustomFieldValue"), isA(Map.class)
-        ).will(
-                returnValue(EasyList.build(
-                        new MockGenericValue("CustomField", EasyMap.build("stringvalue", "foo")),
-                        new MockGenericValue("CustomField", EasyMap.build("stringvalue", "bar"))
-                ))
-        );
-        mockOfBizDelegator.expects(exactly(2)).method("storeAll").with(isA(List.class));
-        ofBizDelegator = (OfBizDelegator) mockOfBizDelegator.proxy();
-
-        mockGenericConfigManager = new Mock(GenericConfigManager.class);
-        mockGenericConfigManager.expects(exactly(3)).method("retrieve").with(
-                eq("Integer"), eq("labels-upgrade")
-        ).will(
-                onConsecutiveCalls(
-                        returnValue(null),
-                        returnValue(0),
-                        returnValue(1)
-                )
-        );
-        mockGenericConfigManager.expects(exactly(3)).method("update").with(
-                eq("Integer"), eq("labels-upgrade"), eq(new Integer(1))
-        );
-        genericConfigManager = (GenericConfigManager) mockGenericConfigManager.proxy();
-
-        mockCustomField = new Mock(CustomField.class);
-        mockCustomField.expects(exactly(2)).method("getId").withNoArguments().will(returnValue("customfield_10000"));
-        mockCustomField.expects(exactly(2)).method("getCustomFieldType").withNoArguments().will(
-                returnValue(
-                        new LabelsCFType(null, null, null, null, null, null, null)
-                )
-        );
-        customField = (CustomField) mockCustomField.proxy();
-
-        mockCustomFieldManager = new Mock(CustomFieldManager.class);
-        mockCustomFieldManager.expects(exactly(2)).method("getCustomFieldObjects").withNoArguments().will(returnValue(EasyList.build(customField)));
-        customFieldManager = (CustomFieldManager) mockCustomFieldManager.proxy();
-
-
-        BackwardsCompatibilitySupport backwardsCompatibilitySupport = new BackwardsCompatibilitySupport() {
-            protected GenericConfigManager createGenericConfigManager(OfBizDelegator delegator) {
-                return genericConfigManager;
-            }
-
-            protected OfBizDelegator getOfBizDelegator(PicoContainer container) {
-                return ofBizDelegator;
-            }
-
-            protected PicoContainer getContainer() {
-                return null;
-            }
-
-            protected CustomFieldManager getCustomFieldManager() {
-                return customFieldManager;
-            }
-        };
-
-        backwardsCompatibilitySupport.start();
-        backwardsCompatibilitySupport.start();
-        backwardsCompatibilitySupport.start();
-
-        mockOfBizDelegator.verify();
-        mockGenericConfigManager.verify();
-        mockCustomFieldManager.verify();
-        
-    }
-}