import org.sonar.db.Database;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
+import org.sonar.db.property.PropertiesDao;
+import org.sonar.db.property.PropertyDto;
import org.sonar.db.version.DatabaseMigration;
import org.sonar.db.version.DatabaseVersion;
import org.sonar.process.ProcessProperties;
return getContainer().getComponentsByType(Footer.class);
}
- public void setGlobalProperty(String key, @Nullable String value) {
- get(PersistentSettings.class).saveProperty(key, value);
+ public void saveProperty(String key, @Nullable Long componentId, @Nullable Long userId, @Nullable String value) {
+ if (componentId == null && userId == null) {
+ get(PersistentSettings.class).saveProperty(key, value);
+ } else {
+ DbClient dbClient = get(DbClient.class);
+ PropertiesDao propertiesDao = dbClient.propertiesDao();
+
+ try (DbSession dbSession = dbClient.openSession(false)) {
+ if (value == null) {
+ propertiesDao.delete(dbSession, new PropertyDto().setKey(key).setResourceId(componentId).setUserId(userId));
+ } else {
+ propertiesDao.saveProperty(dbSession, new PropertyDto().setKey(key).setResourceId(componentId).setUserId(userId).setValue(value));
+ }
+ dbSession.commit();
+ }
+ }
}
public Settings getSettings() {
named_scope :with_keys, lambda { |values| {:conditions => ['prop_key in (?)', values]} }
named_scope :with_key, lambda { |value| {:conditions => {:prop_key, value}} }
named_scope :with_key_prefix, lambda { |value| {:conditions => ['prop_key like ?', value + '%']} }
- named_scope :with_value, lambda { |value| {:conditions => ['text_value like ?', value]} }
+ named_scope :with_text_value, lambda { |value| {:conditions => ['text_value = ?', value]} }
+ named_scope :with_clob_value, lambda { |value| {:conditions => ['clob_value like ?', value]} }
named_scope :with_resource, lambda { |value| {:conditions => {:resource_id => value}} }
named_scope :with_user, lambda { |value| {:conditions => {:user_id => value}} }
named_scope :with_resources, :conditions => 'resource_id is not null'
end
def value
- text_value
+ if is_empty?
+ ''
+ elsif text_value.nil?
+ clob_value
+ else
+ text_value
+ end
end
def self.hash(resource_id=nil, user_id=nil)
def self.clear(key, resource_id=nil, user_id=nil)
prop = by_key(key, resource_id, user_id)
if prop
- all(key, resource_id, user_id).delete_all
- setGlobalProperty(key, nil, resource_id, user_id)
+ Api::Utils.java_facade.saveProperty(key, resource_id, user_id, nil)
end
prop
end
def self.clear_for_resources(key, value=nil)
scope = Property.with_resources().with_key(key)
if value
- scope = scope.with_value(value)
+ if value.length > 4000
+ scope = scope.with_clob_value(value)
+ else
+ scope = scope.with_text_value(value)
+ end
end
scope.delete_all
end
property = by_key(key, resource_id, user_id)
return default_value unless property
- property.text_value || default_value
+ property.value || default_value
end
def self.values(key, resource_id=nil, user_id=nil)
value = Property.new({:prop_key => key}).multi_values? ? array_value_to_string(value) : value.first
end
- text_value = value.to_s if defined? value
- text_value = nil if text_value.blank?
+ raw_value = value.to_s if defined? value
+ raw_value = nil if raw_value.blank?
# Load Java property definition
property_def = field_property_def(key) || property_def(key)
- if text_value.blank?
- return Property.clear(key, resource_id)
+ prop = by_key(key, resource_id, user_id)
+ if !prop
+ prop = Property.new(:prop_key => key, :resource_id => resource_id, :user_id => user_id)
end
- prop = by_key(key, resource_id, user_id)
- if prop && prop.text_value == text_value
+ if prop.value == raw_value
return prop
end
- if !prop
- prop = Property.new(:prop_key => key, :resource_id => resource_id, :user_id => user_id)
- # Do not update password that wasn't updated
- elsif property_def.type().to_s == PropertyType::TYPE_PASSWORD && text_value == EXISTING_PASSWORD
- text_value = prop.text_value
+ # Do not update password that wasn't updated
+ if property_def.type().to_s == PropertyType::TYPE_PASSWORD && raw_value == EXISTING_PASSWORD
+ raw_value = prop.value
end
- prop.text_value = text_value
- if prop.save
- setGlobalProperty(key, text_value, resource_id, user_id)
- end
+ # create/update property in DB
+ Api::Utils.java_facade.saveProperty(key, resource_id, user_id, raw_value)
+ # update returned property
+ if raw_value.nil?
+ prop.text_value=''
+ else
+ prop.text_value=raw_value
+ end
prop
end
array.map { |v| v.gsub(',', '%2C') }.join(',')
end
- def self.setGlobalProperty(key, value, resource_id, user_id)
- Api::Utils.java_facade.setGlobalProperty(key, value) unless (resource_id || user_id)
- end
-
private
def self.all(key, resource_id=nil, user_id=nil)
def validate_property
if java_definition
- validation_result = java_definition.validate(text_value)
+ validation_result = java_definition.validate(value)
errors.add_to_base(validation_result.errorKey) unless validation_result.isValid()
end
end
def validate_field
if java_field_definition
- validation_result = java_field_definition.validate(text_value)
+ validation_result = java_field_definition.validate(value)
errors.add_to_base(validation_result.errorKey) unless validation_result.isValid()
end
end