001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.batch.indexer;
021    
022    import org.sonar.api.database.DatabaseSession;
023    import org.sonar.api.database.model.ResourceModel;
024    import org.sonar.api.database.model.Snapshot;
025    import org.sonar.api.resources.Resource;
026    
027    public abstract class ResourcePersister<RESOURCE extends Resource> {
028    
029      private DatabaseSession session;
030    
031      public ResourcePersister(DatabaseSession session) {
032        this.session = session;
033      }
034    
035      protected DatabaseSession getSession() {
036        return session;
037      }
038    
039      public final void persist(Bucket<RESOURCE> bucket) {
040        String effectiveKey = generateEffectiveKey(bucket);
041        ResourceModel model = session.getSingleResult(ResourceModel.class, "key", effectiveKey);
042    
043        RESOURCE resource = bucket.getResource();
044        if (model == null) {
045          model = ResourceModel.build(resource);
046          model.setKey(effectiveKey);
047          
048        } else {
049          // update existing record
050          model.setEnabled(true);
051          model.setName(resource.getName());
052          model.setLongName(resource.getLongName());
053          model.setDescription(resource.getDescription());
054          model.setScope(resource.getScope());
055          model.setQualifier(resource.getQualifier());
056          if (resource.getLanguage() != null) {
057            model.setLanguageKey(resource.getLanguage().getKey());
058          } 
059        }
060    
061        prepareResourceModel(model, bucket);
062        model = session.save(model);
063        resource.setId(model.getId());
064        resource.setEffectiveKey(model.getKey());
065    
066        Snapshot snapshot = createSnapshot(bucket, model);
067        if (snapshot.getId()==null) {
068          session.save(snapshot);
069        }
070        bucket.setSnapshot(snapshot);
071      }
072    
073      protected abstract void prepareResourceModel(ResourceModel resourceModel, Bucket<RESOURCE> bucket);
074    
075      protected abstract Snapshot createSnapshot(Bucket<RESOURCE> bucket, ResourceModel resourceModel);
076    
077      protected abstract String generateEffectiveKey(Bucket<RESOURCE> bucket);
078    }