JIRA DC Scriptrunner - change the name of a custom field context - jira

JIRA Data Center v9.x
using Scriptrunner, I can get the context name of a custom field
CustomField cField = customFieldManager.getCustomFieldObject(customFieldId)
List<FieldConfigScheme> contexts = cField.getConfigurationSchemes()
if ( contexts.size() == 1 ){
FieldConfigScheme tmpContext = contexts[0]
log.warn("tmpContext :: ${tmpContext.name}")
}
I need to be able to change the context name!
The FieldConfigScheme class does not have a set...
I have looked through all the related classes I can find, and done Google searches, all to no avail.
is there a way to change the custom field context name using Scriptrunner?
That did the trick
This is what I got working!
def allCFIDs = customFieldManager.getCustomFieldObjects().collect({return it.getId()})
allCFIDs.each { customFieldId ->
CustomField cField =
customFieldManager.getCustomFieldObject(customFieldId)
List<FieldConfigScheme> contexts = cField.getConfigurationSchemes()
FieldConfigScheme tmpContext = contexts[0]
if ( tmpContext.name.contains("Fred") ){
def newname = tmpContext.name + " Flintstone"
log.warn("Rename Context:: ${tmpContext.name} to :: ${newname}")
def schemeData = new SchemeBuilder( tmpContext )
schemeData.setName( newname )
def updatedScheme = schemeData.toFieldConfigScheme()
fieldConfigSchemeManager.updateFieldConfigScheme( updatedScheme )
}
}

Related

Hi Team, am new in groovy, i know that its simple, but I got confused.in my controller i have 2list of values. thats shown like

Ratios:[2,9,3.5]//double value// AND uom:[1,2,3,4]
now need to get these through map like this,
Ratios:[2]`Ratios:[9]`Ratios:[3.5]`
uom:[1]`uom:[2]`uom:[3]`uom:[4]`
and Finally need to associate all these in one array , LIKE
MyArray[ Ratios:[2]
Ratios:[9]
Ratios:[3.5]
uom:[1]
uom:[2]
uom:[3]
uom:[4] ]
how do i get like this.PLEASE HELP ME
my code is,
def jsonSlurperjson = new groovy.json.JsonSlurper();
def jsonData = [];
def Ratios;
def UOM;
def RatioMap=[];
def Obj = jsonSlurperjson.parseText(params.Selected);
if (Obj.uom!= null || Obj.uom!= "") {
UOM= Obj.uom;
}
if (Obj.ratio != null || Obj.ratio != "") {
Ratios = Obj.ratio;
}
for(int i=0; i<Ratios.size(); i++) {
RatioMap.add(Ratios[i]);
}
You can collect over both lists to create the maps and add the two
resulting lists to get the final list
[2,9,3.5].collect{ [Ratios: it] } + [1,2,3,4].collect{ [uom: it] }
// → [[Ratios:2], [Ratios:9], [Ratios:3.5], [uom:1], [uom:2], [uom:3], [uom:4]]

In F#, how to update/replace a nested record using a list index?

(Totally Newbie).
Please assume the following in F#
module Visit =
type Model =
{ Name: string }
module Cell =
type Model =
{ Visit: Visit.Model option }
module Column =
type Model =
{ AppointmentCount: int
InnerRows: Cell.Model list }
module App =
...stuff with List.tryFind to return an open column ...
let AddVisit (c:Column.Model, v:Visit) =
{ c with c.InnerRows[AppointmentCount] = { c.InnerRows[AppointmentCount] with Visit = v } }
Assuming there will be 4 cells per column, the Visit is supplied by a database read, and the column instance is found through a couple of List.tryFind's, can a nested record (i.e., the visit of a cell) be replaced/updated with the AppointmentCount as an index?
That is, this fails:
let AddVisit (c:Column.Model, v:Visit) =
{ c with c.InnerRows[AppointmentCount] = { c.InnerRows[AppointmentCount] with Visit = v } }
Error: Field bindings must have the form 'id = expr,'
Thank you.
Take for instance a newly created record.
let myRecord2 = { MyRecord.X = 1; MyRecord.Y = 2; MyRecord.Z = 3 }
To update only two fields in that record you can use the copy and update record expression:
let myRecord3 = { myRecord2 with Y = 100; Z = 2 }
Copy and Update Record Expressions
let AddVisit (c:Column.Model, v:Visit) =
{ c.InnerRows.[c.AppointmentCount] with Visit = Some v }
Note that c.InnerRows.[c.AppointmentCount] specifies a specific cell.model to which the Visit will be set. (Also, the "." in front of the [ allows for direct indexing into the list.

Calculate no of attachments and show it in tree view in openerp 7.0

I am using the following code to add a new column in stock.picking.in object and update the no of attachments to it (to show in tree view the count of attachments).
class stock_picking(osv.osv):
_inherit = "stock.picking.in"
_name = 'stock.picking.in'
def count_attachments(self, cr, uid, ids, fields, arg, context):
obj_attachment = self.pool.get('ir.attachment')
for record in self:
_logger.info("record now in tree view:"+record)
record.attachment_count =0
attachment_ids = obj_attachment.search([('res_model','=','stock.picking.in'),('res_id','=',record.id)]).ids
if attachment_ids:
record.attachment_count =len(attachment_ids)
return record
_columns = {
'attachment_count' :fields.function(count_attachments,method=True,string="Attachment Count" ,type='integer')
}
stock_picking()
Then I have added the following line to the tree view.
<field name="attachment_count">
to show the count in tree view.
However the values are not getting updated and the count_attachments is not getting called.
Any help is appreciated. Thanks in advance!
Try following,
class stock_picking(osv.osv):
_inherit = "stock.picking.in"
_name = 'stock.picking.in'
def count_attachments(self, cr, uid, ids, fields, arg, context=None):
obj_attachment = self.pool.get('ir.attachment')
res = {}
for record in self:
res[record.id] = 0
_logger.info("record now in tree view:"+record)
attachment_ids = obj_attachment.search([('res_model','=','stock.picking.in'),('res_id','=',record.id)]).ids
if attachment_ids:
res[record.id] = len(attachment_ids)
return res
_columns = {
'attachment_count' :fields.function(count_attachments,method=True,string="Attachment Count" ,type='integer')
}

Copy Object Properties to a Map by Value not by Reference

I'm not sure where i'm going wrong, but it seems that I'm not able to copy properties from an object instance and assign them to a map without the values being changed after saving the instance.
This is a sample class:
class Product {
String productName
String proudctDescription
int quantityOnHand
}
Once the form is submitted and it's sent to my controller, I can access the values and manipulate them from the productInstance.properties map that is available from the instance. I want to copy the properties to another map to preserve the values before committing them during an edit. So let's say we are editing a record and these are the values stored in the db: productName = "My Product", productDescription = "My Product Description" and quantityOnHand = 100.
I want to copy them to:
def propertiesBefore = productInstance.properties
This did not work, because when I save the productInstance, the values in propertiesBefore change to whatever the instance had.
So I tried this:
productInstance.properties.each { k,v -> propertiesBefore[k] = v }
Same thing happened again. I am not sure how to copy by value, it seems no matter what I try it copies by reference instead.
EDIT
As per the request of Pawel P., this is the code that I tested:
class Product {
String productName
String productDescription
int quantityOnHand
}
def productInstance = new Product(productName: "Some name", productDescription: "Desciption", quantityOnHand: 10)
def propertiesBefore = [:]
productInstance.properties.each { k,v -> propertiesBefore[k] = (v instanceof Cloneable) ? v.clone() : v }
productInstance.productName = "x"
productInstance.productDescription = "y"
productInstance.quantityOnHand = 9
println propertiesBefore.quantityOnHand // this will print the same as the one after the save()
productInstance.save(flush:true)
println propertiesBefore.quantityOnHand // this will print the same as the one above the save()
Without cloning, copying hash-map [:]'s values to a new hash-map [:]'s space can also be done by "pushing" the first one over, which would achieve the same result that you desired (copy by value)!
def APE = [:]
APE= [tail: 1, body: "hairy", hungry: "VERY!!!"]
def CAVEMAN = [:]
CAVEMAN << APE //push APE to CAVEMAN's space
//modify APE's values for CAVEMAN
CAVEMAN.tail = 0
CAVEMAN.body = "need clothes"
println "'APE': ${APE}"
println "'CAVEMAN': ${CAVEMAN}"
Output ==>
'APE': [tail:1, body:hairy, hungry:VERY!!!]
'CAVEMAN': [tail:0, body:need clothes, hungry:VERY!!!]
The problem is that you actually copy references to variables. To obtain copy of variable you should use clone(). Take a look:
class Product {
String productName
String productDescription
int quantityOnHand
}
def productInstance = new Product(productName: "Some name", productDescription: "Desciption", quantityOnHand: 10)
def propertiesBefore = [:]
productInstance.properties.each { k,v -> propertiesBefore[k] = (v instanceof Cloneable) ? v.clone() : v }
productInstance.productName = "x"
productInstance.productDescription = "y"
productInstance.quantityOnHand = 9
println productInstance.properties
println propertiesBefore
It prints:
[quantityOnHand:9, class:class Product, productName:x, productDescription:y]
[quantityOnHand:10, class:class Product, productName:Some name, productDescription:Desciption]
A simpler example for groovy using Hash-Map [:] can be like this:
def APE = [:]
APE= [tail: 1, body: "hairy", hungry: "VERY!!!"]
def CloneMe = APE //*APE as clone*
def CAVEMAN = [:] //*copy APE's values over thru mapping the clone*
CloneMe.each { key,value -> CAVEMAN[key] = (value instanceof Cloneable) ? value.clone() : value }
println "'CloneMe': ${CloneMe}"
//change some of the clone's values for CAVEMAN
CAVEMAN.tail = 0
CAVEMAN.body = "need clothes"
println "'APE': ${APE}"
println "'CAVEMAN': ${CAVEMAN}"
Output ==>
'CloneMe': [tail:1, body:hairy, hungry:VERY!!!]
'APE': [tail:1, body:hairy, hungry:VERY!!!]
'CAVEMAN': [tail:0, body:need clothes, hungry:VERY!!!]

IBM DOORS Copy text from column

I'm new to DOORS and DXL scripting (not sure if it'll be needed here or not). What I'm looking to do is create a new column in two separate modules that will copy the Absolute Numbers. I know this sounds simple enough, but what I'm running into an issue with is that I use a tool to convert my modules into a PDF and it combines them into one module before doing so. Doing this messes up the Absolute Numbers and I can't afford to have that happen.
More descriptive:
I have a column that contains the following:
'REQ01: 4' where 'REQ01:' represents the module and '4' represents the Absolute Number. Similarly, I have 'REQ02: 4'. I need to copy those in their respective modules and make sure they don't change after the modules have been combined.
I've tried my hand at some DXL scripting and this is what I came up with:
displayRich("REQ01: " obj."Absolute Number" "")
This appropriately shows the column, but again will not work as the Absolute Number ends up changing when I merge the modules.
Thanks in advanced for your help and I apologize if I left any crucial information out.
Here is the code that ultimately worked for me.
// DXL generated on 11 June 2014 by Attribute DXL wizard, Version 1.0
// The wizard will use the following comments on subsequent runs
// to determine the selected options and attributes. It will not
// take account of any manual changes in the body of the DXL code.
// begin parameters (do not edit)
// One attribute/property per line: true
// Show attribute/property names: false
// Include OLE objects in text: true
// Attribute: Object Text
// end parameters (do not edit)
Module m
AttrDef ad
AttrType at
m = module obj
ad = find(m,attrDXLName)
if(!null ad && ad.object)
{
at = ad.type
if(at.type == attrText)
{
string s
Buffer val = create
Buffer temp = create
ad = find(m,"Object Text")
if(!null ad)
{
probeRichAttr_(obj,"Object Identifier", temp, true)
val += tempStringOf temp
}
obj.attrDXLName = richText (tempStringOf val)
delete val
delete temp
}
}
There's a builtin "Copy Attributes" script which does this. At least in the version installed at my company, it's in the PSToolbox, and also available in the menu PSToolbox/attributes/copy.../betweenattributes... Here you go:
// Copy values from one attribute to another
/*
*/
/*
PSToolbox Tools for customizing DOORS with DXL V7.1
-------------------------------------------------
DISCLAIMER:
This programming tool has been thoroughly checked
and tested at all stages of its production.
Telelogic cannot accept any responsibility for
any loss, disruption or damage to your data or
your computer system that may occur while using
this script.
If you do not accept these conditions, do not use
this customised script.
-------------------------------------------------
*/
if ( !confirm "This script copies values from one attribute to another in the same module.\n" //-
"Use this to assist in changing the types of attributes.\n\n" //-
"It asks you to select the source and destination attributes.\n\n" //-
"It works by ... well, copying attribute values!\n\n" //-
"Continue?"
)
{
halt
}
#include <addins/PSToolbox/utensils/dbs.inc>
const int MAXATTRS = 1000
DB copyAttrValsDB = null
DBE copyAttrValsFrom = null
DBE copyAttrValsTo = null
DBE copyAttrValsConfirm = null
DBE copyAttrValsButt = null
string attrListFrom[MAXATTRS]
string attrListTo[MAXATTRS]
string confirmOpts[] = { "Yes", "Yes to all", "No", "No to all", "Cancel" }
bool confirmDataLoss = true
bool noToDataLoss = false
///////////////////////////////////////////////////////////
// Call-backs
void doAttrValsCopy(DB db) {
// check attributes
string fromAn = attrListFrom[get copyAttrValsFrom]
string toAn = attrListTo [get copyAttrValsTo ]
if ( fromAn == toAn ) {
ack "Cannot copy attribute to itself."
return
}
// get confirmation
if ( !confirm "Confirm copy of attribute '" fromAn "' to attribute '" toAn "'." ) {
return
}
confirmDataLoss = get copyAttrValsConfirm
// do copy
Object o
for o in current Module do
{
Buffer oldVal = create()
Buffer newVal = create()
if ( fromAn == "Object Identifier" ) newVal = identifier(o)
else if ( fromAn == "Object Level" ) newVal = level(o) ""
else if ( fromAn == "Object Number" ) newVal = number(o)
else newVal = richText o.fromAn
oldVal = richText o.toAn
if ( confirmDataLoss && !noToDataLoss && length(oldVal) > 0 && oldVal != newVal )
{
current = o
refresh current
int opt = query("About to lose attribute '" toAn "' = '" stringOf(oldVal) "' on current object.", confirmOpts)
if ( opt == 1 ) confirmDataLoss = false
if ( opt == 2 ) continue
if ( opt == 3 ) noToDataLoss = true
if ( opt == 4 ) return
}
if ( !confirmDataLoss || !noToDataLoss || length(oldVal) == 0 ) o.toAn = richText stringOf(newVal)
delete(oldVal)
delete(newVal)
}
hide copyAttrValsDB
}
///////////////////////////////////////////////////////////
// Main program
int numAttrsFrom = 0
int numAttrsTo = 0
AttrDef ad
for ad in current Module do {
if ( ad.module ) continue
string an = ad.name
if ( canRead (current Module, an) ) attrListFrom[numAttrsFrom++] = an
if ( canWrite(current Module, an) ) attrListTo [numAttrsTo++ ] = an
}
attrListFrom[numAttrsFrom++] = "Object Identifier"
attrListFrom[numAttrsFrom++] = "Object Level"
attrListFrom[numAttrsFrom++] = "Object Number"
copyAttrValsDB = create "Copy attribute values"
copyAttrValsFrom = choice(copyAttrValsDB, "From:", attrListFrom, numAttrsFrom, 0)
copyAttrValsTo = choice(copyAttrValsDB, "To:", attrListTo, numAttrsTo, 0)
copyAttrValsButt = apply (copyAttrValsDB, "Copy", doAttrValsCopy)
copyAttrValsConfirm = toggle(copyAttrValsDB, "Confirm on loss of data", true)
show copyAttrValsDB

Resources