| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package com.xpn.xwiki.internal.store; |
| 21 |
|
|
| 22 |
|
import java.util.Arrays; |
| 23 |
|
import java.util.List; |
| 24 |
|
|
| 25 |
|
import javax.inject.Inject; |
| 26 |
|
import javax.inject.Singleton; |
| 27 |
|
|
| 28 |
|
import org.slf4j.Logger; |
| 29 |
|
import org.xwiki.component.annotation.Component; |
| 30 |
|
|
| 31 |
|
import com.xpn.xwiki.objects.BaseProperty; |
| 32 |
|
import com.xpn.xwiki.objects.classes.ListClass; |
| 33 |
|
import com.xpn.xwiki.objects.classes.NumberClass; |
| 34 |
|
import com.xpn.xwiki.objects.classes.PropertyClass; |
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
@version |
| 47 |
|
@since |
| 48 |
|
|
| 49 |
|
@Component(roles = PropertyConverter.class) |
| 50 |
|
@Singleton |
| |
|
| 79.7% |
Uncovered Elements: 13 (64) |
Complexity: 20 |
Complexity Density: 0.56 |
|
| 51 |
|
public class PropertyConverter |
| 52 |
|
{ |
| 53 |
|
@Inject |
| 54 |
|
private Logger logger; |
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
@param |
| 60 |
|
@param |
| 61 |
|
@return |
| 62 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 4 |
Complexity Density: 0.33 |
|
| 63 |
9 |
public BaseProperty<?> convertProperty(BaseProperty<?> storedProperty, PropertyClass modifiedPropertyClass)... |
| 64 |
|
{ |
| 65 |
9 |
Object newValue = convertPropertyValue(storedProperty.getValue(), modifiedPropertyClass); |
| 66 |
9 |
BaseProperty<?> newProperty = null; |
| 67 |
9 |
if (newValue != null) { |
| 68 |
6 |
newProperty = modifiedPropertyClass.newProperty(); |
| 69 |
6 |
try { |
| 70 |
|
|
| 71 |
6 |
newProperty.setValue(newValue); |
| 72 |
|
} catch (Exception e) { |
| 73 |
|
|
| 74 |
|
|
| 75 |
3 |
newProperty = modifiedPropertyClass.fromString(storedProperty.toText()); |
| 76 |
|
} |
| 77 |
6 |
if (newProperty != null) { |
| 78 |
5 |
newProperty.setId(storedProperty.getId()); |
| 79 |
5 |
newProperty.setName(storedProperty.getName()); |
| 80 |
|
} else { |
| 81 |
|
|
| 82 |
1 |
this.logger.warn("Incompatible data migration when changing field [{}] of class [{}]", |
| 83 |
|
modifiedPropertyClass.getName(), modifiedPropertyClass.getClassName()); |
| 84 |
|
} |
| 85 |
|
} else { |
| 86 |
|
|
| 87 |
|
} |
| 88 |
9 |
return newProperty; |
| 89 |
|
} |
| 90 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
| 91 |
9 |
private Object convertPropertyValue(Object storedValue, PropertyClass modifiedPropertyClass)... |
| 92 |
|
{ |
| 93 |
9 |
if (modifiedPropertyClass instanceof ListClass) { |
| 94 |
4 |
return convertPropertyValue(storedValue, (ListClass) modifiedPropertyClass); |
| 95 |
5 |
} else if (modifiedPropertyClass instanceof NumberClass) { |
| 96 |
4 |
return convertPropertyValue(storedValue, (NumberClass) modifiedPropertyClass); |
| 97 |
|
} else { |
| 98 |
|
|
| 99 |
|
|
| 100 |
1 |
return storedValue; |
| 101 |
|
} |
| 102 |
|
} |
| 103 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 7 |
Complexity Density: 1.17 |
|
| 104 |
4 |
private Object convertPropertyValue(Object storedValue, ListClass modifiedListClass)... |
| 105 |
|
{ |
| 106 |
4 |
if (modifiedListClass.isMultiSelect() && !(storedValue instanceof List) && storedValue != null) { |
| 107 |
|
|
| 108 |
1 |
return Arrays.asList(storedValue); |
| 109 |
3 |
} else if (!modifiedListClass.isMultiSelect() && storedValue instanceof List) { |
| 110 |
|
|
| 111 |
2 |
@SuppressWarnings("unchecked") |
| 112 |
|
List<String> oldValues = (List<String>) storedValue; |
| 113 |
2 |
return oldValues.isEmpty() ? null : oldValues.get(0); |
| 114 |
|
} |
| 115 |
|
|
| 116 |
1 |
return storedValue; |
| 117 |
|
} |
| 118 |
|
|
| |
|
| 43.5% |
Uncovered Elements: 13 (23) |
Complexity: 6 |
Complexity Density: 0.46 |
|
| 119 |
4 |
private Object convertPropertyValue(Object storedValue, NumberClass modifiedNumberClass)... |
| 120 |
|
{ |
| 121 |
4 |
Object newValue = storedValue; |
| 122 |
4 |
if (storedValue instanceof Number) { |
| 123 |
|
|
| 124 |
1 |
Number storedNumber = (Number) storedValue; |
| 125 |
1 |
String newNumberType = modifiedNumberClass.getNumberType(); |
| 126 |
1 |
if ("integer".equals(newNumberType)) { |
| 127 |
1 |
newValue = Integer.valueOf(storedNumber.intValue()); |
| 128 |
0 |
} else if ("float".equals(newNumberType)) { |
| 129 |
0 |
newValue = Float.valueOf(storedNumber.floatValue()); |
| 130 |
0 |
} else if ("double".equals(newNumberType)) { |
| 131 |
0 |
newValue = Double.valueOf(storedNumber.doubleValue()); |
| 132 |
0 |
} else if ("long".equals(newNumberType)) { |
| 133 |
0 |
newValue = Long.valueOf(storedNumber.longValue()); |
| 134 |
|
} |
| 135 |
|
} else { |
| 136 |
|
|
| 137 |
|
|
| 138 |
|
|
| 139 |
|
} |
| 140 |
4 |
return newValue; |
| 141 |
|
} |
| 142 |
|
} |