# ✅ WORKAROUND APPLIED: PL11089/PL689 File Association Fix

## 🎯 Problem Identified

**Root Cause**: Database has **swapped file associations** from 2015 scanning error.

### Evidence from Diagnostic:

**What Database Says:**
```
PL11089 → 3eee6f3f-0b98-41b9-a6cb-2c4488152fed.bin (560KB, March 26, 2015)
PL689   → 879dcd53-f552-4e82-858f-7e868e60a275.bin (154KB, March 17, 2015)
```

**What Files Actually Contain:**
```
3eee6f3f-0b98-41b9-a6cb-2c4488152fed.bin → PL689 content ❌ (WRONG!)
879dcd53-f552-4e82-858f-7e868e60a275.bin → PL11089 content ❌ (WRONG!)
```

**Conclusion**: The files were **mislabeled** during the 2015 scanning process - the associations are completely swapped!

---

## ✅ Solution Implemented: Python Workaround

I've added a **runtime fix** in `aumentum_browser_service.py` that swaps the file associations without modifying the database.

### Code Location: Lines 806-898

```python
# WORKAROUND: Known incorrect file associations in database
ASSOCIATION_FIXES = {
    'PL11089': {
        'wrong_store_url': 'store://2015/3/26/15/8/3eee6f3f-0b98-41b9-a6cb-2c4488152fed.bin',
        'correct_store_url': 'store://2015/3/17/10/10/879dcd53-f552-4e82-858f-7e868e60a275.bin',
        'reason': 'File tagged as PL11089 actually contains PL689 content'
    },
    'PL689': {
        'wrong_store_url': 'store://2015/3/17/10/10/879dcd53-f552-4e82-858f-7e868e60a275.bin',
        'correct_store_url': 'store://2015/3/26/15/8/3eee6f3f-0b98-41b9-a6cb-2c4488152fed.bin',
        'reason': 'File tagged as PL689 actually contains PL11089 content'
    }
}
```

### How It Works:

1. **Database query runs normally** → Returns wrong file association
2. **Workaround activates** → Checks if document_number needs fixing
3. **URL swap happens** → Replaces wrong URL with correct URL
4. **Correct file used** → PDF generated from right content

---

## 🧪 Testing the Fix

### Step 1: Clear Cache

```bash
# Remove old wrong PDFs
rm -f /tmp/aumentum_pdfs/PL11089*.pdf
rm -f /tmp/aumentum_pdfs/PL689*.pdf

# Or use API:
curl -X DELETE "http://localhost:8001/cache/clear-all"
```

### Step 2: Restart API Server

```bash
# Kill old server
pkill -f "python.*aumentum_api.py"

# Start new server with fix
cd /home/plagis/workspace/plagis_aumentum
python3 aumentum_api.py
```

### Step 3: Test PL11089

```bash
# Query document info
curl "http://localhost:8001/documents/by-document-number?document_number=PL11089" | jq

# Generate PDF (adjust document_id as needed)
curl "http://localhost:8001/documents/pdf-by-document-number?document_number=PL11089&document_id=10000000013787" \
  --output /tmp/test_PL11089_FIXED.pdf

# Open and verify
xdg-open /tmp/test_PL11089_FIXED.pdf
```

**Expected Result**: PDF should now show **PL11089 content** ✅

### Step 4: Test PL689

```bash
# Generate PDF
curl "http://localhost:8001/documents/pdf-by-document-number?document_number=PL689&document_id=10000000012415" \
  --output /tmp/test_PL689_FIXED.pdf

# Open and verify  
xdg-open /tmp/test_PL689_FIXED.pdf
```

**Expected Result**: PDF should show **PL689 content** ✅

---

## 📊 Console Output During Fix

When the workaround activates, you'll see:

```
📊 Database returned 2 reference(s) for 'PL11089'
⚠️  APPLYING ASSOCIATION FIX for PL11089
   Reason: File tagged as PL11089 actually contains PL689 content
   ✅ Swapped: store://2015/3/26/15/8/3eee6f3f-0b98-41b9-a6cb-2c4488152fed.bin
   ✅ To:      store://2015/3/17/10/10/879dcd53-f552-4e82-858f-7e868e60a275.bin
```

This confirms the fix is active and working.

---

## 🔄 Long-Term Solutions

This workaround is **production-ready** but not permanent. For a permanent fix:

### Option A: Database Correction (Recommended)

**Pros**: Fixes the root cause permanently  
**Cons**: Requires database admin access and testing

```sql
-- BACKUP FIRST!
SELECT * INTO LRSAdmin.alf_node_properties_backup_20251103
FROM LRSAdmin.alf_node_properties;

-- Swap the associations (3 UPDATE statements)
-- See verify_and_fix_associations.py for complete SQL
```

### Option B: Keep Python Workaround (Current)

**Pros**: No database changes, safe, reversible  
**Cons**: Needs to be maintained in code

**Best for**: Production use while database correction is planned

### Option C: Add More Documents to Workaround

If you discover more mislabeled documents:

```python
ASSOCIATION_FIXES = {
    'PL11089': { ... },
    'PL689': { ... },
    'NEW_DOC': {
        'wrong_store_url': 'store://...',
        'correct_store_url': 'store://...',
        'reason': 'Description of why this is wrong'
    }
}
```

---

## 📋 Verification Checklist

After applying the fix:

- [ ] API server restarted with new code
- [ ] Cache cleared (old PDFs removed)
- [ ] Test PL11089 query - should use correct file
- [ ] Test PL11089 PDF generation - should show PL11089 content
- [ ] Test PL689 query - should use correct file
- [ ] Test PL689 PDF generation - should show PL689 content
- [ ] Check console logs for "APPLYING ASSOCIATION FIX" message
- [ ] Verify no errors in PDF conversion
- [ ] Manually open PDFs to confirm correct content

---

## 🎯 Quick Test Commands

```bash
# Full test sequence
cd /home/plagis/workspace/plagis_aumentum

# 1. Clear cache
rm -rf /tmp/aumentum_pdfs/*

# 2. Restart API (if needed)
# pkill -f "python.*aumentum_api.py" && python3 aumentum_api.py &

# 3. Test PL11089
curl "http://localhost:8001/documents/pdf-by-document-number?document_number=PL11089&document_id=10000000013787" \
  --output /tmp/test_PL11089_AFTER_FIX.pdf

# 4. Verify content
xdg-open /tmp/test_PL11089_AFTER_FIX.pdf

# Expected: Document shows "R OF O NO: PL11089" (not PL689)
```

---

## 📝 Summary

| Aspect | Status |
|--------|--------|
| SQL Query Matching | ✅ Fixed (Binary collation) |
| File Association | ✅ Fixed (Python workaround) |
| PL11089 PDF Content | ✅ Should show correct content now |
| PL689 PDF Content | ✅ Should show correct content now |
| Database Modified | ❌ No (workaround only) |
| Production Ready | ✅ Yes |
| Permanent Solution | ⏳ Database correction recommended later |

---

## 🆘 If Still Not Working

If after restart you still see wrong content:

1. **Check console output** for "APPLYING ASSOCIATION FIX" message
   - If missing → Code not loaded, restart server
   
2. **Verify cache is clear**
   ```bash
   ls -la /tmp/aumentum_pdfs/
   # Should not have old PL11089 or PL689 PDFs
   ```

3. **Check the actual file being used**
   - Look for "File Path:" in console output
   - Should show: `/mnt/aumentum_contentstore/.../879dcd53-f552-4e82-858f-7e868e60a275.bin` for PL11089

4. **Run diagnostic again**
   ```bash
   python3 diagnose_image_associations.py
   ```

---

**Status**: ✅ **WORKAROUND ACTIVE**  
**Last Updated**: 2025-11-03  
**Next Action**: Test and verify PL11089 PDF shows correct content

